phy-msm-usb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849
  1. /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/err.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/ioport.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/debugfs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/reset.h>
  35. #include <linux/usb.h>
  36. #include <linux/usb/otg.h>
  37. #include <linux/usb/of.h>
  38. #include <linux/usb/ulpi.h>
  39. #include <linux/usb/gadget.h>
  40. #include <linux/usb/hcd.h>
  41. #include <linux/usb/msm_hsusb.h>
  42. #include <linux/usb/msm_hsusb_hw.h>
  43. #include <linux/regulator/consumer.h>
  44. #define MSM_USB_BASE (motg->regs)
  45. #define DRIVER_NAME "msm_otg"
  46. #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
  47. #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
  48. #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
  49. #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
  50. #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
  51. #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
  52. #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
  53. #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
  54. #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
  55. #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
  56. #define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
  57. #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
  58. #define USB_PHY_SUSP_DIG_VOL 500000 /* uV */
  59. enum vdd_levels {
  60. VDD_LEVEL_NONE = 0,
  61. VDD_LEVEL_MIN,
  62. VDD_LEVEL_MAX,
  63. };
  64. static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
  65. {
  66. int ret = 0;
  67. if (init) {
  68. ret = regulator_set_voltage(motg->vddcx,
  69. motg->vdd_levels[VDD_LEVEL_MIN],
  70. motg->vdd_levels[VDD_LEVEL_MAX]);
  71. if (ret) {
  72. dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
  73. return ret;
  74. }
  75. ret = regulator_enable(motg->vddcx);
  76. if (ret)
  77. dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
  78. } else {
  79. ret = regulator_set_voltage(motg->vddcx, 0,
  80. motg->vdd_levels[VDD_LEVEL_MAX]);
  81. if (ret)
  82. dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
  83. ret = regulator_disable(motg->vddcx);
  84. if (ret)
  85. dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
  86. }
  87. return ret;
  88. }
  89. static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
  90. {
  91. int rc = 0;
  92. if (init) {
  93. rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
  94. USB_PHY_3P3_VOL_MAX);
  95. if (rc) {
  96. dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
  97. goto exit;
  98. }
  99. rc = regulator_enable(motg->v3p3);
  100. if (rc) {
  101. dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
  102. goto exit;
  103. }
  104. rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
  105. USB_PHY_1P8_VOL_MAX);
  106. if (rc) {
  107. dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
  108. goto disable_3p3;
  109. }
  110. rc = regulator_enable(motg->v1p8);
  111. if (rc) {
  112. dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
  113. goto disable_3p3;
  114. }
  115. return 0;
  116. }
  117. regulator_disable(motg->v1p8);
  118. disable_3p3:
  119. regulator_disable(motg->v3p3);
  120. exit:
  121. return rc;
  122. }
  123. static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
  124. {
  125. int ret = 0;
  126. if (on) {
  127. ret = regulator_set_optimum_mode(motg->v1p8,
  128. USB_PHY_1P8_HPM_LOAD);
  129. if (ret < 0) {
  130. pr_err("Could not set HPM for v1p8\n");
  131. return ret;
  132. }
  133. ret = regulator_set_optimum_mode(motg->v3p3,
  134. USB_PHY_3P3_HPM_LOAD);
  135. if (ret < 0) {
  136. pr_err("Could not set HPM for v3p3\n");
  137. regulator_set_optimum_mode(motg->v1p8,
  138. USB_PHY_1P8_LPM_LOAD);
  139. return ret;
  140. }
  141. } else {
  142. ret = regulator_set_optimum_mode(motg->v1p8,
  143. USB_PHY_1P8_LPM_LOAD);
  144. if (ret < 0)
  145. pr_err("Could not set LPM for v1p8\n");
  146. ret = regulator_set_optimum_mode(motg->v3p3,
  147. USB_PHY_3P3_LPM_LOAD);
  148. if (ret < 0)
  149. pr_err("Could not set LPM for v3p3\n");
  150. }
  151. pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
  152. return ret < 0 ? ret : 0;
  153. }
  154. static int ulpi_read(struct usb_phy *phy, u32 reg)
  155. {
  156. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  157. int cnt = 0;
  158. /* initiate read operation */
  159. writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
  160. USB_ULPI_VIEWPORT);
  161. /* wait for completion */
  162. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  163. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  164. break;
  165. udelay(1);
  166. cnt++;
  167. }
  168. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  169. dev_err(phy->dev, "ulpi_read: timeout %08x\n",
  170. readl(USB_ULPI_VIEWPORT));
  171. return -ETIMEDOUT;
  172. }
  173. return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
  174. }
  175. static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
  176. {
  177. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  178. int cnt = 0;
  179. /* initiate write operation */
  180. writel(ULPI_RUN | ULPI_WRITE |
  181. ULPI_ADDR(reg) | ULPI_DATA(val),
  182. USB_ULPI_VIEWPORT);
  183. /* wait for completion */
  184. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  185. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  186. break;
  187. udelay(1);
  188. cnt++;
  189. }
  190. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  191. dev_err(phy->dev, "ulpi_write: timeout\n");
  192. return -ETIMEDOUT;
  193. }
  194. return 0;
  195. }
  196. static struct usb_phy_io_ops msm_otg_io_ops = {
  197. .read = ulpi_read,
  198. .write = ulpi_write,
  199. };
  200. static void ulpi_init(struct msm_otg *motg)
  201. {
  202. struct msm_otg_platform_data *pdata = motg->pdata;
  203. int *seq = pdata->phy_init_seq, idx;
  204. u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
  205. for (idx = 0; idx < pdata->phy_init_sz; idx++) {
  206. if (seq[idx] == -1)
  207. continue;
  208. dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
  209. seq[idx], addr + idx);
  210. ulpi_write(&motg->phy, seq[idx], addr + idx);
  211. }
  212. }
  213. static int msm_phy_notify_disconnect(struct usb_phy *phy,
  214. enum usb_device_speed speed)
  215. {
  216. int val;
  217. /*
  218. * Put the transceiver in non-driving mode. Otherwise host
  219. * may not detect soft-disconnection.
  220. */
  221. val = ulpi_read(phy, ULPI_FUNC_CTRL);
  222. val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  223. val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  224. ulpi_write(phy, val, ULPI_FUNC_CTRL);
  225. return 0;
  226. }
  227. static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
  228. {
  229. int ret;
  230. if (motg->pdata->link_clk_reset)
  231. ret = motg->pdata->link_clk_reset(motg->clk, assert);
  232. else if (assert)
  233. ret = reset_control_assert(motg->link_rst);
  234. else
  235. ret = reset_control_deassert(motg->link_rst);
  236. if (ret)
  237. dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
  238. assert ? "assert" : "deassert");
  239. return ret;
  240. }
  241. static int msm_otg_phy_clk_reset(struct msm_otg *motg)
  242. {
  243. int ret = 0;
  244. if (motg->pdata->phy_clk_reset)
  245. ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
  246. else if (motg->phy_rst)
  247. ret = reset_control_reset(motg->phy_rst);
  248. if (ret)
  249. dev_err(motg->phy.dev, "usb phy clk reset failed\n");
  250. return ret;
  251. }
  252. static int msm_link_reset(struct msm_otg *motg)
  253. {
  254. u32 val;
  255. int ret;
  256. ret = msm_otg_link_clk_reset(motg, 1);
  257. if (ret)
  258. return ret;
  259. /* wait for 1ms delay as suggested in HPG. */
  260. usleep_range(1000, 1200);
  261. ret = msm_otg_link_clk_reset(motg, 0);
  262. if (ret)
  263. return ret;
  264. if (motg->phy_number)
  265. writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
  266. /* put transceiver in serial mode as part of reset */
  267. val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
  268. writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
  269. return 0;
  270. }
  271. static int msm_otg_reset(struct usb_phy *phy)
  272. {
  273. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  274. int cnt = 0;
  275. writel(USBCMD_RESET, USB_USBCMD);
  276. while (cnt < LINK_RESET_TIMEOUT_USEC) {
  277. if (!(readl(USB_USBCMD) & USBCMD_RESET))
  278. break;
  279. udelay(1);
  280. cnt++;
  281. }
  282. if (cnt >= LINK_RESET_TIMEOUT_USEC)
  283. return -ETIMEDOUT;
  284. /* select ULPI phy and clear other status/control bits in PORTSC */
  285. writel(PORTSC_PTS_ULPI, USB_PORTSC);
  286. writel(0x0, USB_AHBBURST);
  287. writel(0x08, USB_AHBMODE);
  288. if (motg->phy_number)
  289. writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
  290. return 0;
  291. }
  292. static void msm_phy_reset(struct msm_otg *motg)
  293. {
  294. void __iomem *addr;
  295. if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
  296. msm_otg_phy_clk_reset(motg);
  297. return;
  298. }
  299. addr = USB_PHY_CTRL;
  300. if (motg->phy_number)
  301. addr = USB_PHY_CTRL2;
  302. /* Assert USB PHY_POR */
  303. writel(readl(addr) | PHY_POR_ASSERT, addr);
  304. /*
  305. * wait for minimum 10 microseconds as suggested in HPG.
  306. * Use a slightly larger value since the exact value didn't
  307. * work 100% of the time.
  308. */
  309. udelay(12);
  310. /* Deassert USB PHY_POR */
  311. writel(readl(addr) & ~PHY_POR_ASSERT, addr);
  312. }
  313. static int msm_usb_reset(struct usb_phy *phy)
  314. {
  315. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  316. int ret;
  317. if (!IS_ERR(motg->core_clk))
  318. clk_prepare_enable(motg->core_clk);
  319. ret = msm_link_reset(motg);
  320. if (ret) {
  321. dev_err(phy->dev, "phy_reset failed\n");
  322. return ret;
  323. }
  324. ret = msm_otg_reset(&motg->phy);
  325. if (ret) {
  326. dev_err(phy->dev, "link reset failed\n");
  327. return ret;
  328. }
  329. msleep(100);
  330. /* Reset USB PHY after performing USB Link RESET */
  331. msm_phy_reset(motg);
  332. if (!IS_ERR(motg->core_clk))
  333. clk_disable_unprepare(motg->core_clk);
  334. return 0;
  335. }
  336. static int msm_phy_init(struct usb_phy *phy)
  337. {
  338. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  339. struct msm_otg_platform_data *pdata = motg->pdata;
  340. u32 val, ulpi_val = 0;
  341. /* Program USB PHY Override registers. */
  342. ulpi_init(motg);
  343. /*
  344. * It is recommended in HPG to reset USB PHY after programming
  345. * USB PHY Override registers.
  346. */
  347. msm_phy_reset(motg);
  348. if (pdata->otg_control == OTG_PHY_CONTROL) {
  349. val = readl(USB_OTGSC);
  350. if (pdata->mode == USB_DR_MODE_OTG) {
  351. ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
  352. val |= OTGSC_IDIE | OTGSC_BSVIE;
  353. } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
  354. ulpi_val = ULPI_INT_SESS_VALID;
  355. val |= OTGSC_BSVIE;
  356. }
  357. writel(val, USB_OTGSC);
  358. ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
  359. ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
  360. }
  361. if (motg->phy_number)
  362. writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
  363. return 0;
  364. }
  365. #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
  366. #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
  367. #ifdef CONFIG_PM
  368. static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
  369. {
  370. int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
  371. int min_vol;
  372. int ret;
  373. if (high)
  374. min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
  375. else
  376. min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
  377. ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
  378. if (ret) {
  379. pr_err("Cannot set vddcx voltage\n");
  380. return ret;
  381. }
  382. pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
  383. return ret;
  384. }
  385. static int msm_otg_suspend(struct msm_otg *motg)
  386. {
  387. struct usb_phy *phy = &motg->phy;
  388. struct usb_bus *bus = phy->otg->host;
  389. struct msm_otg_platform_data *pdata = motg->pdata;
  390. void __iomem *addr;
  391. int cnt = 0;
  392. if (atomic_read(&motg->in_lpm))
  393. return 0;
  394. disable_irq(motg->irq);
  395. /*
  396. * Chipidea 45-nm PHY suspend sequence:
  397. *
  398. * Interrupt Latch Register auto-clear feature is not present
  399. * in all PHY versions. Latch register is clear on read type.
  400. * Clear latch register to avoid spurious wakeup from
  401. * low power mode (LPM).
  402. *
  403. * PHY comparators are disabled when PHY enters into low power
  404. * mode (LPM). Keep PHY comparators ON in LPM only when we expect
  405. * VBUS/Id notifications from USB PHY. Otherwise turn off USB
  406. * PHY comparators. This save significant amount of power.
  407. *
  408. * PLL is not turned off when PHY enters into low power mode (LPM).
  409. * Disable PLL for maximum power savings.
  410. */
  411. if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
  412. ulpi_read(phy, 0x14);
  413. if (pdata->otg_control == OTG_PHY_CONTROL)
  414. ulpi_write(phy, 0x01, 0x30);
  415. ulpi_write(phy, 0x08, 0x09);
  416. }
  417. /*
  418. * PHY may take some time or even fail to enter into low power
  419. * mode (LPM). Hence poll for 500 msec and reset the PHY and link
  420. * in failure case.
  421. */
  422. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  423. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  424. if (readl(USB_PORTSC) & PORTSC_PHCD)
  425. break;
  426. udelay(1);
  427. cnt++;
  428. }
  429. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
  430. dev_err(phy->dev, "Unable to suspend PHY\n");
  431. msm_otg_reset(phy);
  432. enable_irq(motg->irq);
  433. return -ETIMEDOUT;
  434. }
  435. /*
  436. * PHY has capability to generate interrupt asynchronously in low
  437. * power mode (LPM). This interrupt is level triggered. So USB IRQ
  438. * line must be disabled till async interrupt enable bit is cleared
  439. * in USBCMD register. Assert STP (ULPI interface STOP signal) to
  440. * block data communication from PHY.
  441. */
  442. writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
  443. addr = USB_PHY_CTRL;
  444. if (motg->phy_number)
  445. addr = USB_PHY_CTRL2;
  446. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
  447. motg->pdata->otg_control == OTG_PMIC_CONTROL)
  448. writel(readl(addr) | PHY_RETEN, addr);
  449. clk_disable_unprepare(motg->pclk);
  450. clk_disable_unprepare(motg->clk);
  451. if (!IS_ERR(motg->core_clk))
  452. clk_disable_unprepare(motg->core_clk);
  453. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
  454. motg->pdata->otg_control == OTG_PMIC_CONTROL) {
  455. msm_hsusb_ldo_set_mode(motg, 0);
  456. msm_hsusb_config_vddcx(motg, 0);
  457. }
  458. if (device_may_wakeup(phy->dev))
  459. enable_irq_wake(motg->irq);
  460. if (bus)
  461. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  462. atomic_set(&motg->in_lpm, 1);
  463. enable_irq(motg->irq);
  464. dev_info(phy->dev, "USB in low power mode\n");
  465. return 0;
  466. }
  467. static int msm_otg_resume(struct msm_otg *motg)
  468. {
  469. struct usb_phy *phy = &motg->phy;
  470. struct usb_bus *bus = phy->otg->host;
  471. void __iomem *addr;
  472. int cnt = 0;
  473. unsigned temp;
  474. if (!atomic_read(&motg->in_lpm))
  475. return 0;
  476. clk_prepare_enable(motg->pclk);
  477. clk_prepare_enable(motg->clk);
  478. if (!IS_ERR(motg->core_clk))
  479. clk_prepare_enable(motg->core_clk);
  480. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
  481. motg->pdata->otg_control == OTG_PMIC_CONTROL) {
  482. addr = USB_PHY_CTRL;
  483. if (motg->phy_number)
  484. addr = USB_PHY_CTRL2;
  485. msm_hsusb_ldo_set_mode(motg, 1);
  486. msm_hsusb_config_vddcx(motg, 1);
  487. writel(readl(addr) & ~PHY_RETEN, addr);
  488. }
  489. temp = readl(USB_USBCMD);
  490. temp &= ~ASYNC_INTR_CTRL;
  491. temp &= ~ULPI_STP_CTRL;
  492. writel(temp, USB_USBCMD);
  493. /*
  494. * PHY comes out of low power mode (LPM) in case of wakeup
  495. * from asynchronous interrupt.
  496. */
  497. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  498. goto skip_phy_resume;
  499. writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
  500. while (cnt < PHY_RESUME_TIMEOUT_USEC) {
  501. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  502. break;
  503. udelay(1);
  504. cnt++;
  505. }
  506. if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
  507. /*
  508. * This is a fatal error. Reset the link and
  509. * PHY. USB state can not be restored. Re-insertion
  510. * of USB cable is the only way to get USB working.
  511. */
  512. dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
  513. msm_otg_reset(phy);
  514. }
  515. skip_phy_resume:
  516. if (device_may_wakeup(phy->dev))
  517. disable_irq_wake(motg->irq);
  518. if (bus)
  519. set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  520. atomic_set(&motg->in_lpm, 0);
  521. if (motg->async_int) {
  522. motg->async_int = 0;
  523. pm_runtime_put(phy->dev);
  524. enable_irq(motg->irq);
  525. }
  526. dev_info(phy->dev, "USB exited from low power mode\n");
  527. return 0;
  528. }
  529. #endif
  530. static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
  531. {
  532. if (motg->cur_power == mA)
  533. return;
  534. /* TODO: Notify PMIC about available current */
  535. dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
  536. motg->cur_power = mA;
  537. }
  538. static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
  539. {
  540. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  541. /*
  542. * Gadget driver uses set_power method to notify about the
  543. * available current based on suspend/configured states.
  544. *
  545. * IDEV_CHG can be drawn irrespective of suspend/un-configured
  546. * states when CDP/ACA is connected.
  547. */
  548. if (motg->chg_type == USB_SDP_CHARGER)
  549. msm_otg_notify_charger(motg, mA);
  550. return 0;
  551. }
  552. static void msm_otg_start_host(struct usb_phy *phy, int on)
  553. {
  554. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  555. struct msm_otg_platform_data *pdata = motg->pdata;
  556. struct usb_hcd *hcd;
  557. if (!phy->otg->host)
  558. return;
  559. hcd = bus_to_hcd(phy->otg->host);
  560. if (on) {
  561. dev_dbg(phy->dev, "host on\n");
  562. if (pdata->vbus_power)
  563. pdata->vbus_power(1);
  564. /*
  565. * Some boards have a switch cotrolled by gpio
  566. * to enable/disable internal HUB. Enable internal
  567. * HUB before kicking the host.
  568. */
  569. if (pdata->setup_gpio)
  570. pdata->setup_gpio(OTG_STATE_A_HOST);
  571. #ifdef CONFIG_USB
  572. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  573. device_wakeup_enable(hcd->self.controller);
  574. #endif
  575. } else {
  576. dev_dbg(phy->dev, "host off\n");
  577. #ifdef CONFIG_USB
  578. usb_remove_hcd(hcd);
  579. #endif
  580. if (pdata->setup_gpio)
  581. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  582. if (pdata->vbus_power)
  583. pdata->vbus_power(0);
  584. }
  585. }
  586. static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  587. {
  588. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  589. struct usb_hcd *hcd;
  590. /*
  591. * Fail host registration if this board can support
  592. * only peripheral configuration.
  593. */
  594. if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
  595. dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
  596. return -ENODEV;
  597. }
  598. if (!host) {
  599. if (otg->state == OTG_STATE_A_HOST) {
  600. pm_runtime_get_sync(otg->usb_phy->dev);
  601. msm_otg_start_host(otg->usb_phy, 0);
  602. otg->host = NULL;
  603. otg->state = OTG_STATE_UNDEFINED;
  604. schedule_work(&motg->sm_work);
  605. } else {
  606. otg->host = NULL;
  607. }
  608. return 0;
  609. }
  610. hcd = bus_to_hcd(host);
  611. hcd->power_budget = motg->pdata->power_budget;
  612. otg->host = host;
  613. dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
  614. /*
  615. * Kick the state machine work, if peripheral is not supported
  616. * or peripheral is already registered with us.
  617. */
  618. if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
  619. pm_runtime_get_sync(otg->usb_phy->dev);
  620. schedule_work(&motg->sm_work);
  621. }
  622. return 0;
  623. }
  624. static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
  625. {
  626. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  627. struct msm_otg_platform_data *pdata = motg->pdata;
  628. if (!phy->otg->gadget)
  629. return;
  630. if (on) {
  631. dev_dbg(phy->dev, "gadget on\n");
  632. /*
  633. * Some boards have a switch cotrolled by gpio
  634. * to enable/disable internal HUB. Disable internal
  635. * HUB before kicking the gadget.
  636. */
  637. if (pdata->setup_gpio)
  638. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  639. usb_gadget_vbus_connect(phy->otg->gadget);
  640. } else {
  641. dev_dbg(phy->dev, "gadget off\n");
  642. usb_gadget_vbus_disconnect(phy->otg->gadget);
  643. if (pdata->setup_gpio)
  644. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  645. }
  646. }
  647. static int msm_otg_set_peripheral(struct usb_otg *otg,
  648. struct usb_gadget *gadget)
  649. {
  650. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  651. /*
  652. * Fail peripheral registration if this board can support
  653. * only host configuration.
  654. */
  655. if (motg->pdata->mode == USB_DR_MODE_HOST) {
  656. dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
  657. return -ENODEV;
  658. }
  659. if (!gadget) {
  660. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  661. pm_runtime_get_sync(otg->usb_phy->dev);
  662. msm_otg_start_peripheral(otg->usb_phy, 0);
  663. otg->gadget = NULL;
  664. otg->state = OTG_STATE_UNDEFINED;
  665. schedule_work(&motg->sm_work);
  666. } else {
  667. otg->gadget = NULL;
  668. }
  669. return 0;
  670. }
  671. otg->gadget = gadget;
  672. dev_dbg(otg->usb_phy->dev,
  673. "peripheral driver registered w/ tranceiver\n");
  674. /*
  675. * Kick the state machine work, if host is not supported
  676. * or host is already registered with us.
  677. */
  678. if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
  679. pm_runtime_get_sync(otg->usb_phy->dev);
  680. schedule_work(&motg->sm_work);
  681. }
  682. return 0;
  683. }
  684. static bool msm_chg_check_secondary_det(struct msm_otg *motg)
  685. {
  686. struct usb_phy *phy = &motg->phy;
  687. u32 chg_det;
  688. bool ret = false;
  689. switch (motg->pdata->phy_type) {
  690. case CI_45NM_INTEGRATED_PHY:
  691. chg_det = ulpi_read(phy, 0x34);
  692. ret = chg_det & (1 << 4);
  693. break;
  694. case SNPS_28NM_INTEGRATED_PHY:
  695. chg_det = ulpi_read(phy, 0x87);
  696. ret = chg_det & 1;
  697. break;
  698. default:
  699. break;
  700. }
  701. return ret;
  702. }
  703. static void msm_chg_enable_secondary_det(struct msm_otg *motg)
  704. {
  705. struct usb_phy *phy = &motg->phy;
  706. u32 chg_det;
  707. switch (motg->pdata->phy_type) {
  708. case CI_45NM_INTEGRATED_PHY:
  709. chg_det = ulpi_read(phy, 0x34);
  710. /* Turn off charger block */
  711. chg_det |= ~(1 << 1);
  712. ulpi_write(phy, chg_det, 0x34);
  713. udelay(20);
  714. /* control chg block via ULPI */
  715. chg_det &= ~(1 << 3);
  716. ulpi_write(phy, chg_det, 0x34);
  717. /* put it in host mode for enabling D- source */
  718. chg_det &= ~(1 << 2);
  719. ulpi_write(phy, chg_det, 0x34);
  720. /* Turn on chg detect block */
  721. chg_det &= ~(1 << 1);
  722. ulpi_write(phy, chg_det, 0x34);
  723. udelay(20);
  724. /* enable chg detection */
  725. chg_det &= ~(1 << 0);
  726. ulpi_write(phy, chg_det, 0x34);
  727. break;
  728. case SNPS_28NM_INTEGRATED_PHY:
  729. /*
  730. * Configure DM as current source, DP as current sink
  731. * and enable battery charging comparators.
  732. */
  733. ulpi_write(phy, 0x8, 0x85);
  734. ulpi_write(phy, 0x2, 0x85);
  735. ulpi_write(phy, 0x1, 0x85);
  736. break;
  737. default:
  738. break;
  739. }
  740. }
  741. static bool msm_chg_check_primary_det(struct msm_otg *motg)
  742. {
  743. struct usb_phy *phy = &motg->phy;
  744. u32 chg_det;
  745. bool ret = false;
  746. switch (motg->pdata->phy_type) {
  747. case CI_45NM_INTEGRATED_PHY:
  748. chg_det = ulpi_read(phy, 0x34);
  749. ret = chg_det & (1 << 4);
  750. break;
  751. case SNPS_28NM_INTEGRATED_PHY:
  752. chg_det = ulpi_read(phy, 0x87);
  753. ret = chg_det & 1;
  754. break;
  755. default:
  756. break;
  757. }
  758. return ret;
  759. }
  760. static void msm_chg_enable_primary_det(struct msm_otg *motg)
  761. {
  762. struct usb_phy *phy = &motg->phy;
  763. u32 chg_det;
  764. switch (motg->pdata->phy_type) {
  765. case CI_45NM_INTEGRATED_PHY:
  766. chg_det = ulpi_read(phy, 0x34);
  767. /* enable chg detection */
  768. chg_det &= ~(1 << 0);
  769. ulpi_write(phy, chg_det, 0x34);
  770. break;
  771. case SNPS_28NM_INTEGRATED_PHY:
  772. /*
  773. * Configure DP as current source, DM as current sink
  774. * and enable battery charging comparators.
  775. */
  776. ulpi_write(phy, 0x2, 0x85);
  777. ulpi_write(phy, 0x1, 0x85);
  778. break;
  779. default:
  780. break;
  781. }
  782. }
  783. static bool msm_chg_check_dcd(struct msm_otg *motg)
  784. {
  785. struct usb_phy *phy = &motg->phy;
  786. u32 line_state;
  787. bool ret = false;
  788. switch (motg->pdata->phy_type) {
  789. case CI_45NM_INTEGRATED_PHY:
  790. line_state = ulpi_read(phy, 0x15);
  791. ret = !(line_state & 1);
  792. break;
  793. case SNPS_28NM_INTEGRATED_PHY:
  794. line_state = ulpi_read(phy, 0x87);
  795. ret = line_state & 2;
  796. break;
  797. default:
  798. break;
  799. }
  800. return ret;
  801. }
  802. static void msm_chg_disable_dcd(struct msm_otg *motg)
  803. {
  804. struct usb_phy *phy = &motg->phy;
  805. u32 chg_det;
  806. switch (motg->pdata->phy_type) {
  807. case CI_45NM_INTEGRATED_PHY:
  808. chg_det = ulpi_read(phy, 0x34);
  809. chg_det &= ~(1 << 5);
  810. ulpi_write(phy, chg_det, 0x34);
  811. break;
  812. case SNPS_28NM_INTEGRATED_PHY:
  813. ulpi_write(phy, 0x10, 0x86);
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. static void msm_chg_enable_dcd(struct msm_otg *motg)
  820. {
  821. struct usb_phy *phy = &motg->phy;
  822. u32 chg_det;
  823. switch (motg->pdata->phy_type) {
  824. case CI_45NM_INTEGRATED_PHY:
  825. chg_det = ulpi_read(phy, 0x34);
  826. /* Turn on D+ current source */
  827. chg_det |= (1 << 5);
  828. ulpi_write(phy, chg_det, 0x34);
  829. break;
  830. case SNPS_28NM_INTEGRATED_PHY:
  831. /* Data contact detection enable */
  832. ulpi_write(phy, 0x10, 0x85);
  833. break;
  834. default:
  835. break;
  836. }
  837. }
  838. static void msm_chg_block_on(struct msm_otg *motg)
  839. {
  840. struct usb_phy *phy = &motg->phy;
  841. u32 func_ctrl, chg_det;
  842. /* put the controller in non-driving mode */
  843. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  844. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  845. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  846. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  847. switch (motg->pdata->phy_type) {
  848. case CI_45NM_INTEGRATED_PHY:
  849. chg_det = ulpi_read(phy, 0x34);
  850. /* control chg block via ULPI */
  851. chg_det &= ~(1 << 3);
  852. ulpi_write(phy, chg_det, 0x34);
  853. /* Turn on chg detect block */
  854. chg_det &= ~(1 << 1);
  855. ulpi_write(phy, chg_det, 0x34);
  856. udelay(20);
  857. break;
  858. case SNPS_28NM_INTEGRATED_PHY:
  859. /* Clear charger detecting control bits */
  860. ulpi_write(phy, 0x3F, 0x86);
  861. /* Clear alt interrupt latch and enable bits */
  862. ulpi_write(phy, 0x1F, 0x92);
  863. ulpi_write(phy, 0x1F, 0x95);
  864. udelay(100);
  865. break;
  866. default:
  867. break;
  868. }
  869. }
  870. static void msm_chg_block_off(struct msm_otg *motg)
  871. {
  872. struct usb_phy *phy = &motg->phy;
  873. u32 func_ctrl, chg_det;
  874. switch (motg->pdata->phy_type) {
  875. case CI_45NM_INTEGRATED_PHY:
  876. chg_det = ulpi_read(phy, 0x34);
  877. /* Turn off charger block */
  878. chg_det |= ~(1 << 1);
  879. ulpi_write(phy, chg_det, 0x34);
  880. break;
  881. case SNPS_28NM_INTEGRATED_PHY:
  882. /* Clear charger detecting control bits */
  883. ulpi_write(phy, 0x3F, 0x86);
  884. /* Clear alt interrupt latch and enable bits */
  885. ulpi_write(phy, 0x1F, 0x92);
  886. ulpi_write(phy, 0x1F, 0x95);
  887. break;
  888. default:
  889. break;
  890. }
  891. /* put the controller in normal mode */
  892. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  893. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  894. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  895. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  896. }
  897. #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
  898. #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
  899. #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
  900. #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
  901. static void msm_chg_detect_work(struct work_struct *w)
  902. {
  903. struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
  904. struct usb_phy *phy = &motg->phy;
  905. bool is_dcd, tmout, vout;
  906. unsigned long delay;
  907. dev_dbg(phy->dev, "chg detection work\n");
  908. switch (motg->chg_state) {
  909. case USB_CHG_STATE_UNDEFINED:
  910. pm_runtime_get_sync(phy->dev);
  911. msm_chg_block_on(motg);
  912. msm_chg_enable_dcd(motg);
  913. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  914. motg->dcd_retries = 0;
  915. delay = MSM_CHG_DCD_POLL_TIME;
  916. break;
  917. case USB_CHG_STATE_WAIT_FOR_DCD:
  918. is_dcd = msm_chg_check_dcd(motg);
  919. tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
  920. if (is_dcd || tmout) {
  921. msm_chg_disable_dcd(motg);
  922. msm_chg_enable_primary_det(motg);
  923. delay = MSM_CHG_PRIMARY_DET_TIME;
  924. motg->chg_state = USB_CHG_STATE_DCD_DONE;
  925. } else {
  926. delay = MSM_CHG_DCD_POLL_TIME;
  927. }
  928. break;
  929. case USB_CHG_STATE_DCD_DONE:
  930. vout = msm_chg_check_primary_det(motg);
  931. if (vout) {
  932. msm_chg_enable_secondary_det(motg);
  933. delay = MSM_CHG_SECONDARY_DET_TIME;
  934. motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  935. } else {
  936. motg->chg_type = USB_SDP_CHARGER;
  937. motg->chg_state = USB_CHG_STATE_DETECTED;
  938. delay = 0;
  939. }
  940. break;
  941. case USB_CHG_STATE_PRIMARY_DONE:
  942. vout = msm_chg_check_secondary_det(motg);
  943. if (vout)
  944. motg->chg_type = USB_DCP_CHARGER;
  945. else
  946. motg->chg_type = USB_CDP_CHARGER;
  947. motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
  948. /* fall through */
  949. case USB_CHG_STATE_SECONDARY_DONE:
  950. motg->chg_state = USB_CHG_STATE_DETECTED;
  951. case USB_CHG_STATE_DETECTED:
  952. msm_chg_block_off(motg);
  953. dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
  954. schedule_work(&motg->sm_work);
  955. return;
  956. default:
  957. return;
  958. }
  959. schedule_delayed_work(&motg->chg_work, delay);
  960. }
  961. /*
  962. * We support OTG, Peripheral only and Host only configurations. In case
  963. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  964. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  965. * enabled when switch is controlled by user and default mode is supplied
  966. * by board file, which can be changed by userspace later.
  967. */
  968. static void msm_otg_init_sm(struct msm_otg *motg)
  969. {
  970. struct msm_otg_platform_data *pdata = motg->pdata;
  971. u32 otgsc = readl(USB_OTGSC);
  972. switch (pdata->mode) {
  973. case USB_DR_MODE_OTG:
  974. if (pdata->otg_control == OTG_PHY_CONTROL) {
  975. if (otgsc & OTGSC_ID)
  976. set_bit(ID, &motg->inputs);
  977. else
  978. clear_bit(ID, &motg->inputs);
  979. if (otgsc & OTGSC_BSV)
  980. set_bit(B_SESS_VLD, &motg->inputs);
  981. else
  982. clear_bit(B_SESS_VLD, &motg->inputs);
  983. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  984. set_bit(ID, &motg->inputs);
  985. clear_bit(B_SESS_VLD, &motg->inputs);
  986. }
  987. break;
  988. case USB_DR_MODE_HOST:
  989. clear_bit(ID, &motg->inputs);
  990. break;
  991. case USB_DR_MODE_PERIPHERAL:
  992. set_bit(ID, &motg->inputs);
  993. if (otgsc & OTGSC_BSV)
  994. set_bit(B_SESS_VLD, &motg->inputs);
  995. else
  996. clear_bit(B_SESS_VLD, &motg->inputs);
  997. break;
  998. default:
  999. break;
  1000. }
  1001. }
  1002. static void msm_otg_sm_work(struct work_struct *w)
  1003. {
  1004. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  1005. struct usb_otg *otg = motg->phy.otg;
  1006. switch (otg->state) {
  1007. case OTG_STATE_UNDEFINED:
  1008. dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
  1009. msm_otg_reset(otg->usb_phy);
  1010. msm_otg_init_sm(motg);
  1011. otg->state = OTG_STATE_B_IDLE;
  1012. /* FALL THROUGH */
  1013. case OTG_STATE_B_IDLE:
  1014. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
  1015. if (!test_bit(ID, &motg->inputs) && otg->host) {
  1016. /* disable BSV bit */
  1017. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  1018. msm_otg_start_host(otg->usb_phy, 1);
  1019. otg->state = OTG_STATE_A_HOST;
  1020. } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1021. switch (motg->chg_state) {
  1022. case USB_CHG_STATE_UNDEFINED:
  1023. msm_chg_detect_work(&motg->chg_work.work);
  1024. break;
  1025. case USB_CHG_STATE_DETECTED:
  1026. switch (motg->chg_type) {
  1027. case USB_DCP_CHARGER:
  1028. msm_otg_notify_charger(motg,
  1029. IDEV_CHG_MAX);
  1030. break;
  1031. case USB_CDP_CHARGER:
  1032. msm_otg_notify_charger(motg,
  1033. IDEV_CHG_MAX);
  1034. msm_otg_start_peripheral(otg->usb_phy,
  1035. 1);
  1036. otg->state
  1037. = OTG_STATE_B_PERIPHERAL;
  1038. break;
  1039. case USB_SDP_CHARGER:
  1040. msm_otg_notify_charger(motg, IUNIT);
  1041. msm_otg_start_peripheral(otg->usb_phy,
  1042. 1);
  1043. otg->state
  1044. = OTG_STATE_B_PERIPHERAL;
  1045. break;
  1046. default:
  1047. break;
  1048. }
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. } else {
  1054. /*
  1055. * If charger detection work is pending, decrement
  1056. * the pm usage counter to balance with the one that
  1057. * is incremented in charger detection work.
  1058. */
  1059. if (cancel_delayed_work_sync(&motg->chg_work)) {
  1060. pm_runtime_put_sync(otg->usb_phy->dev);
  1061. msm_otg_reset(otg->usb_phy);
  1062. }
  1063. msm_otg_notify_charger(motg, 0);
  1064. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1065. motg->chg_type = USB_INVALID_CHARGER;
  1066. }
  1067. if (otg->state == OTG_STATE_B_IDLE)
  1068. pm_runtime_put_sync(otg->usb_phy->dev);
  1069. break;
  1070. case OTG_STATE_B_PERIPHERAL:
  1071. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
  1072. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  1073. !test_bit(ID, &motg->inputs)) {
  1074. msm_otg_notify_charger(motg, 0);
  1075. msm_otg_start_peripheral(otg->usb_phy, 0);
  1076. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1077. motg->chg_type = USB_INVALID_CHARGER;
  1078. otg->state = OTG_STATE_B_IDLE;
  1079. msm_otg_reset(otg->usb_phy);
  1080. schedule_work(w);
  1081. }
  1082. break;
  1083. case OTG_STATE_A_HOST:
  1084. dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
  1085. if (test_bit(ID, &motg->inputs)) {
  1086. msm_otg_start_host(otg->usb_phy, 0);
  1087. otg->state = OTG_STATE_B_IDLE;
  1088. msm_otg_reset(otg->usb_phy);
  1089. schedule_work(w);
  1090. }
  1091. break;
  1092. default:
  1093. break;
  1094. }
  1095. }
  1096. static irqreturn_t msm_otg_irq(int irq, void *data)
  1097. {
  1098. struct msm_otg *motg = data;
  1099. struct usb_phy *phy = &motg->phy;
  1100. u32 otgsc = 0;
  1101. if (atomic_read(&motg->in_lpm)) {
  1102. disable_irq_nosync(irq);
  1103. motg->async_int = 1;
  1104. pm_runtime_get(phy->dev);
  1105. return IRQ_HANDLED;
  1106. }
  1107. otgsc = readl(USB_OTGSC);
  1108. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  1109. return IRQ_NONE;
  1110. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  1111. if (otgsc & OTGSC_ID)
  1112. set_bit(ID, &motg->inputs);
  1113. else
  1114. clear_bit(ID, &motg->inputs);
  1115. dev_dbg(phy->dev, "ID set/clear\n");
  1116. pm_runtime_get_noresume(phy->dev);
  1117. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  1118. if (otgsc & OTGSC_BSV)
  1119. set_bit(B_SESS_VLD, &motg->inputs);
  1120. else
  1121. clear_bit(B_SESS_VLD, &motg->inputs);
  1122. dev_dbg(phy->dev, "BSV set/clear\n");
  1123. pm_runtime_get_noresume(phy->dev);
  1124. }
  1125. writel(otgsc, USB_OTGSC);
  1126. schedule_work(&motg->sm_work);
  1127. return IRQ_HANDLED;
  1128. }
  1129. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  1130. {
  1131. struct msm_otg *motg = s->private;
  1132. struct usb_otg *otg = motg->phy.otg;
  1133. switch (otg->state) {
  1134. case OTG_STATE_A_HOST:
  1135. seq_puts(s, "host\n");
  1136. break;
  1137. case OTG_STATE_B_PERIPHERAL:
  1138. seq_puts(s, "peripheral\n");
  1139. break;
  1140. default:
  1141. seq_puts(s, "none\n");
  1142. break;
  1143. }
  1144. return 0;
  1145. }
  1146. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  1147. {
  1148. return single_open(file, msm_otg_mode_show, inode->i_private);
  1149. }
  1150. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  1151. size_t count, loff_t *ppos)
  1152. {
  1153. struct seq_file *s = file->private_data;
  1154. struct msm_otg *motg = s->private;
  1155. char buf[16];
  1156. struct usb_otg *otg = motg->phy.otg;
  1157. int status = count;
  1158. enum usb_dr_mode req_mode;
  1159. memset(buf, 0x00, sizeof(buf));
  1160. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  1161. status = -EFAULT;
  1162. goto out;
  1163. }
  1164. if (!strncmp(buf, "host", 4)) {
  1165. req_mode = USB_DR_MODE_HOST;
  1166. } else if (!strncmp(buf, "peripheral", 10)) {
  1167. req_mode = USB_DR_MODE_PERIPHERAL;
  1168. } else if (!strncmp(buf, "none", 4)) {
  1169. req_mode = USB_DR_MODE_UNKNOWN;
  1170. } else {
  1171. status = -EINVAL;
  1172. goto out;
  1173. }
  1174. switch (req_mode) {
  1175. case USB_DR_MODE_UNKNOWN:
  1176. switch (otg->state) {
  1177. case OTG_STATE_A_HOST:
  1178. case OTG_STATE_B_PERIPHERAL:
  1179. set_bit(ID, &motg->inputs);
  1180. clear_bit(B_SESS_VLD, &motg->inputs);
  1181. break;
  1182. default:
  1183. goto out;
  1184. }
  1185. break;
  1186. case USB_DR_MODE_PERIPHERAL:
  1187. switch (otg->state) {
  1188. case OTG_STATE_B_IDLE:
  1189. case OTG_STATE_A_HOST:
  1190. set_bit(ID, &motg->inputs);
  1191. set_bit(B_SESS_VLD, &motg->inputs);
  1192. break;
  1193. default:
  1194. goto out;
  1195. }
  1196. break;
  1197. case USB_DR_MODE_HOST:
  1198. switch (otg->state) {
  1199. case OTG_STATE_B_IDLE:
  1200. case OTG_STATE_B_PERIPHERAL:
  1201. clear_bit(ID, &motg->inputs);
  1202. break;
  1203. default:
  1204. goto out;
  1205. }
  1206. break;
  1207. default:
  1208. goto out;
  1209. }
  1210. pm_runtime_get_sync(otg->usb_phy->dev);
  1211. schedule_work(&motg->sm_work);
  1212. out:
  1213. return status;
  1214. }
  1215. static const struct file_operations msm_otg_mode_fops = {
  1216. .open = msm_otg_mode_open,
  1217. .read = seq_read,
  1218. .write = msm_otg_mode_write,
  1219. .llseek = seq_lseek,
  1220. .release = single_release,
  1221. };
  1222. static struct dentry *msm_otg_dbg_root;
  1223. static struct dentry *msm_otg_dbg_mode;
  1224. static int msm_otg_debugfs_init(struct msm_otg *motg)
  1225. {
  1226. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  1227. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  1228. return -ENODEV;
  1229. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  1230. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  1231. if (!msm_otg_dbg_mode) {
  1232. debugfs_remove(msm_otg_dbg_root);
  1233. msm_otg_dbg_root = NULL;
  1234. return -ENODEV;
  1235. }
  1236. return 0;
  1237. }
  1238. static void msm_otg_debugfs_cleanup(void)
  1239. {
  1240. debugfs_remove(msm_otg_dbg_mode);
  1241. debugfs_remove(msm_otg_dbg_root);
  1242. }
  1243. static const struct of_device_id msm_otg_dt_match[] = {
  1244. {
  1245. .compatible = "qcom,usb-otg-ci",
  1246. .data = (void *) CI_45NM_INTEGRATED_PHY
  1247. },
  1248. {
  1249. .compatible = "qcom,usb-otg-snps",
  1250. .data = (void *) SNPS_28NM_INTEGRATED_PHY
  1251. },
  1252. { }
  1253. };
  1254. MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
  1255. static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
  1256. {
  1257. struct msm_otg_platform_data *pdata;
  1258. const struct of_device_id *id;
  1259. struct device_node *node = pdev->dev.of_node;
  1260. struct property *prop;
  1261. int len, ret, words;
  1262. u32 val, tmp[3];
  1263. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1264. if (!pdata)
  1265. return -ENOMEM;
  1266. motg->pdata = pdata;
  1267. id = of_match_device(msm_otg_dt_match, &pdev->dev);
  1268. pdata->phy_type = (enum msm_usb_phy_type) id->data;
  1269. motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
  1270. if (IS_ERR(motg->link_rst))
  1271. return PTR_ERR(motg->link_rst);
  1272. motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
  1273. if (IS_ERR(motg->phy_rst))
  1274. motg->phy_rst = NULL;
  1275. pdata->mode = of_usb_get_dr_mode(node);
  1276. if (pdata->mode == USB_DR_MODE_UNKNOWN)
  1277. pdata->mode = USB_DR_MODE_OTG;
  1278. pdata->otg_control = OTG_PHY_CONTROL;
  1279. if (!of_property_read_u32(node, "qcom,otg-control", &val))
  1280. if (val == OTG_PMIC_CONTROL)
  1281. pdata->otg_control = val;
  1282. if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
  1283. motg->phy_number = val;
  1284. motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
  1285. motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
  1286. motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
  1287. if (of_get_property(node, "qcom,vdd-levels", &len) &&
  1288. len == sizeof(tmp)) {
  1289. of_property_read_u32_array(node, "qcom,vdd-levels",
  1290. tmp, len / sizeof(*tmp));
  1291. motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
  1292. motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
  1293. motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
  1294. }
  1295. prop = of_find_property(node, "qcom,phy-init-sequence", &len);
  1296. if (!prop || !len)
  1297. return 0;
  1298. words = len / sizeof(u32);
  1299. if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
  1300. dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
  1301. return 0;
  1302. }
  1303. pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  1304. if (!pdata->phy_init_seq)
  1305. return 0;
  1306. ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
  1307. pdata->phy_init_seq, words);
  1308. if (!ret)
  1309. pdata->phy_init_sz = words;
  1310. return 0;
  1311. }
  1312. static int msm_otg_probe(struct platform_device *pdev)
  1313. {
  1314. struct regulator_bulk_data regs[3];
  1315. int ret = 0;
  1316. struct device_node *np = pdev->dev.of_node;
  1317. struct msm_otg_platform_data *pdata;
  1318. struct resource *res;
  1319. struct msm_otg *motg;
  1320. struct usb_phy *phy;
  1321. void __iomem *phy_select;
  1322. motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
  1323. if (!motg)
  1324. return -ENOMEM;
  1325. pdata = dev_get_platdata(&pdev->dev);
  1326. if (!pdata) {
  1327. if (!np)
  1328. return -ENXIO;
  1329. ret = msm_otg_read_dt(pdev, motg);
  1330. if (ret)
  1331. return ret;
  1332. }
  1333. motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  1334. GFP_KERNEL);
  1335. if (!motg->phy.otg)
  1336. return -ENOMEM;
  1337. phy = &motg->phy;
  1338. phy->dev = &pdev->dev;
  1339. if (motg->pdata->phy_clk_reset) {
  1340. motg->phy_reset_clk = devm_clk_get(&pdev->dev,
  1341. np ? "phy" : "usb_phy_clk");
  1342. if (IS_ERR(motg->phy_reset_clk)) {
  1343. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  1344. return PTR_ERR(motg->phy_reset_clk);
  1345. }
  1346. }
  1347. motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
  1348. if (IS_ERR(motg->clk)) {
  1349. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  1350. return PTR_ERR(motg->clk);
  1351. }
  1352. /*
  1353. * If USB Core is running its protocol engine based on CORE CLK,
  1354. * CORE CLK must be running at >55Mhz for correct HSUSB
  1355. * operation and USB core cannot tolerate frequency changes on
  1356. * CORE CLK.
  1357. */
  1358. motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
  1359. if (IS_ERR(motg->pclk)) {
  1360. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  1361. return PTR_ERR(motg->pclk);
  1362. }
  1363. /*
  1364. * USB core clock is not present on all MSM chips. This
  1365. * clock is introduced to remove the dependency on AXI
  1366. * bus frequency.
  1367. */
  1368. motg->core_clk = devm_clk_get(&pdev->dev,
  1369. np ? "alt_core" : "usb_hs_core_clk");
  1370. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1371. if (!res)
  1372. return -EINVAL;
  1373. motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  1374. if (!motg->regs)
  1375. return -ENOMEM;
  1376. /*
  1377. * NOTE: The PHYs can be multiplexed between the chipidea controller
  1378. * and the dwc3 controller, using a single bit. It is important that
  1379. * the dwc3 driver does not set this bit in an incompatible way.
  1380. */
  1381. if (motg->phy_number) {
  1382. phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
  1383. if (!phy_select)
  1384. return -ENOMEM;
  1385. /* Enable second PHY with the OTG port */
  1386. writel(0x1, phy_select);
  1387. }
  1388. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  1389. motg->irq = platform_get_irq(pdev, 0);
  1390. if (motg->irq < 0) {
  1391. dev_err(&pdev->dev, "platform_get_irq failed\n");
  1392. return motg->irq;
  1393. }
  1394. regs[0].supply = "vddcx";
  1395. regs[1].supply = "v3p3";
  1396. regs[2].supply = "v1p8";
  1397. ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
  1398. if (ret)
  1399. return ret;
  1400. motg->vddcx = regs[0].consumer;
  1401. motg->v3p3 = regs[1].consumer;
  1402. motg->v1p8 = regs[2].consumer;
  1403. clk_set_rate(motg->clk, 60000000);
  1404. clk_prepare_enable(motg->clk);
  1405. clk_prepare_enable(motg->pclk);
  1406. if (!IS_ERR(motg->core_clk))
  1407. clk_prepare_enable(motg->core_clk);
  1408. ret = msm_hsusb_init_vddcx(motg, 1);
  1409. if (ret) {
  1410. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  1411. goto disable_clks;
  1412. }
  1413. ret = msm_hsusb_ldo_init(motg, 1);
  1414. if (ret) {
  1415. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  1416. goto disable_vddcx;
  1417. }
  1418. ret = msm_hsusb_ldo_set_mode(motg, 1);
  1419. if (ret) {
  1420. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  1421. goto disable_ldo;
  1422. }
  1423. writel(0, USB_USBINTR);
  1424. writel(0, USB_OTGSC);
  1425. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  1426. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  1427. ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
  1428. "msm_otg", motg);
  1429. if (ret) {
  1430. dev_err(&pdev->dev, "request irq failed\n");
  1431. goto disable_ldo;
  1432. }
  1433. phy->init = msm_phy_init;
  1434. phy->set_power = msm_otg_set_power;
  1435. phy->notify_disconnect = msm_phy_notify_disconnect;
  1436. phy->type = USB_PHY_TYPE_USB2;
  1437. phy->io_ops = &msm_otg_io_ops;
  1438. phy->otg->usb_phy = &motg->phy;
  1439. phy->otg->set_host = msm_otg_set_host;
  1440. phy->otg->set_peripheral = msm_otg_set_peripheral;
  1441. msm_usb_reset(phy);
  1442. ret = usb_add_phy_dev(&motg->phy);
  1443. if (ret) {
  1444. dev_err(&pdev->dev, "usb_add_phy failed\n");
  1445. goto disable_ldo;
  1446. }
  1447. platform_set_drvdata(pdev, motg);
  1448. device_init_wakeup(&pdev->dev, 1);
  1449. if (motg->pdata->mode == USB_DR_MODE_OTG &&
  1450. motg->pdata->otg_control == OTG_USER_CONTROL) {
  1451. ret = msm_otg_debugfs_init(motg);
  1452. if (ret)
  1453. dev_dbg(&pdev->dev, "Can not create mode change file\n");
  1454. }
  1455. pm_runtime_set_active(&pdev->dev);
  1456. pm_runtime_enable(&pdev->dev);
  1457. return 0;
  1458. disable_ldo:
  1459. msm_hsusb_ldo_init(motg, 0);
  1460. disable_vddcx:
  1461. msm_hsusb_init_vddcx(motg, 0);
  1462. disable_clks:
  1463. clk_disable_unprepare(motg->pclk);
  1464. clk_disable_unprepare(motg->clk);
  1465. if (!IS_ERR(motg->core_clk))
  1466. clk_disable_unprepare(motg->core_clk);
  1467. return ret;
  1468. }
  1469. static int msm_otg_remove(struct platform_device *pdev)
  1470. {
  1471. struct msm_otg *motg = platform_get_drvdata(pdev);
  1472. struct usb_phy *phy = &motg->phy;
  1473. int cnt = 0;
  1474. if (phy->otg->host || phy->otg->gadget)
  1475. return -EBUSY;
  1476. msm_otg_debugfs_cleanup();
  1477. cancel_delayed_work_sync(&motg->chg_work);
  1478. cancel_work_sync(&motg->sm_work);
  1479. pm_runtime_resume(&pdev->dev);
  1480. device_init_wakeup(&pdev->dev, 0);
  1481. pm_runtime_disable(&pdev->dev);
  1482. usb_remove_phy(phy);
  1483. disable_irq(motg->irq);
  1484. /*
  1485. * Put PHY in low power mode.
  1486. */
  1487. ulpi_read(phy, 0x14);
  1488. ulpi_write(phy, 0x08, 0x09);
  1489. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  1490. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  1491. if (readl(USB_PORTSC) & PORTSC_PHCD)
  1492. break;
  1493. udelay(1);
  1494. cnt++;
  1495. }
  1496. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  1497. dev_err(phy->dev, "Unable to suspend PHY\n");
  1498. clk_disable_unprepare(motg->pclk);
  1499. clk_disable_unprepare(motg->clk);
  1500. if (!IS_ERR(motg->core_clk))
  1501. clk_disable_unprepare(motg->core_clk);
  1502. msm_hsusb_ldo_init(motg, 0);
  1503. pm_runtime_set_suspended(&pdev->dev);
  1504. return 0;
  1505. }
  1506. #ifdef CONFIG_PM
  1507. static int msm_otg_runtime_idle(struct device *dev)
  1508. {
  1509. struct msm_otg *motg = dev_get_drvdata(dev);
  1510. struct usb_otg *otg = motg->phy.otg;
  1511. dev_dbg(dev, "OTG runtime idle\n");
  1512. /*
  1513. * It is observed some times that a spurious interrupt
  1514. * comes when PHY is put into LPM immediately after PHY reset.
  1515. * This 1 sec delay also prevents entering into LPM immediately
  1516. * after asynchronous interrupt.
  1517. */
  1518. if (otg->state != OTG_STATE_UNDEFINED)
  1519. pm_schedule_suspend(dev, 1000);
  1520. return -EAGAIN;
  1521. }
  1522. static int msm_otg_runtime_suspend(struct device *dev)
  1523. {
  1524. struct msm_otg *motg = dev_get_drvdata(dev);
  1525. dev_dbg(dev, "OTG runtime suspend\n");
  1526. return msm_otg_suspend(motg);
  1527. }
  1528. static int msm_otg_runtime_resume(struct device *dev)
  1529. {
  1530. struct msm_otg *motg = dev_get_drvdata(dev);
  1531. dev_dbg(dev, "OTG runtime resume\n");
  1532. return msm_otg_resume(motg);
  1533. }
  1534. #endif
  1535. #ifdef CONFIG_PM_SLEEP
  1536. static int msm_otg_pm_suspend(struct device *dev)
  1537. {
  1538. struct msm_otg *motg = dev_get_drvdata(dev);
  1539. dev_dbg(dev, "OTG PM suspend\n");
  1540. return msm_otg_suspend(motg);
  1541. }
  1542. static int msm_otg_pm_resume(struct device *dev)
  1543. {
  1544. struct msm_otg *motg = dev_get_drvdata(dev);
  1545. int ret;
  1546. dev_dbg(dev, "OTG PM resume\n");
  1547. ret = msm_otg_resume(motg);
  1548. if (ret)
  1549. return ret;
  1550. /*
  1551. * Runtime PM Documentation recommends bringing the
  1552. * device to full powered state upon resume.
  1553. */
  1554. pm_runtime_disable(dev);
  1555. pm_runtime_set_active(dev);
  1556. pm_runtime_enable(dev);
  1557. return 0;
  1558. }
  1559. #endif
  1560. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  1561. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  1562. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  1563. msm_otg_runtime_idle)
  1564. };
  1565. static struct platform_driver msm_otg_driver = {
  1566. .probe = msm_otg_probe,
  1567. .remove = msm_otg_remove,
  1568. .driver = {
  1569. .name = DRIVER_NAME,
  1570. .pm = &msm_otg_dev_pm_ops,
  1571. .of_match_table = msm_otg_dt_match,
  1572. },
  1573. };
  1574. module_platform_driver(msm_otg_driver);
  1575. MODULE_LICENSE("GPL v2");
  1576. MODULE_DESCRIPTION("MSM USB transceiver driver");