phy-msm-usb.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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->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->phy->dev, "Host mode is not supported\n");
  596. return -ENODEV;
  597. }
  598. if (!host) {
  599. if (otg->phy->state == OTG_STATE_A_HOST) {
  600. pm_runtime_get_sync(otg->phy->dev);
  601. msm_otg_start_host(otg->phy, 0);
  602. otg->host = NULL;
  603. otg->phy->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->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->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->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->phy->dev, "Peripheral mode is not supported\n");
  657. return -ENODEV;
  658. }
  659. if (!gadget) {
  660. if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
  661. pm_runtime_get_sync(otg->phy->dev);
  662. msm_otg_start_peripheral(otg->phy, 0);
  663. otg->gadget = NULL;
  664. otg->phy->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->phy->dev, "peripheral driver registered w/ tranceiver\n");
  673. /*
  674. * Kick the state machine work, if host is not supported
  675. * or host is already registered with us.
  676. */
  677. if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
  678. pm_runtime_get_sync(otg->phy->dev);
  679. schedule_work(&motg->sm_work);
  680. }
  681. return 0;
  682. }
  683. static bool msm_chg_check_secondary_det(struct msm_otg *motg)
  684. {
  685. struct usb_phy *phy = &motg->phy;
  686. u32 chg_det;
  687. bool ret = false;
  688. switch (motg->pdata->phy_type) {
  689. case CI_45NM_INTEGRATED_PHY:
  690. chg_det = ulpi_read(phy, 0x34);
  691. ret = chg_det & (1 << 4);
  692. break;
  693. case SNPS_28NM_INTEGRATED_PHY:
  694. chg_det = ulpi_read(phy, 0x87);
  695. ret = chg_det & 1;
  696. break;
  697. default:
  698. break;
  699. }
  700. return ret;
  701. }
  702. static void msm_chg_enable_secondary_det(struct msm_otg *motg)
  703. {
  704. struct usb_phy *phy = &motg->phy;
  705. u32 chg_det;
  706. switch (motg->pdata->phy_type) {
  707. case CI_45NM_INTEGRATED_PHY:
  708. chg_det = ulpi_read(phy, 0x34);
  709. /* Turn off charger block */
  710. chg_det |= ~(1 << 1);
  711. ulpi_write(phy, chg_det, 0x34);
  712. udelay(20);
  713. /* control chg block via ULPI */
  714. chg_det &= ~(1 << 3);
  715. ulpi_write(phy, chg_det, 0x34);
  716. /* put it in host mode for enabling D- source */
  717. chg_det &= ~(1 << 2);
  718. ulpi_write(phy, chg_det, 0x34);
  719. /* Turn on chg detect block */
  720. chg_det &= ~(1 << 1);
  721. ulpi_write(phy, chg_det, 0x34);
  722. udelay(20);
  723. /* enable chg detection */
  724. chg_det &= ~(1 << 0);
  725. ulpi_write(phy, chg_det, 0x34);
  726. break;
  727. case SNPS_28NM_INTEGRATED_PHY:
  728. /*
  729. * Configure DM as current source, DP as current sink
  730. * and enable battery charging comparators.
  731. */
  732. ulpi_write(phy, 0x8, 0x85);
  733. ulpi_write(phy, 0x2, 0x85);
  734. ulpi_write(phy, 0x1, 0x85);
  735. break;
  736. default:
  737. break;
  738. }
  739. }
  740. static bool msm_chg_check_primary_det(struct msm_otg *motg)
  741. {
  742. struct usb_phy *phy = &motg->phy;
  743. u32 chg_det;
  744. bool ret = false;
  745. switch (motg->pdata->phy_type) {
  746. case CI_45NM_INTEGRATED_PHY:
  747. chg_det = ulpi_read(phy, 0x34);
  748. ret = chg_det & (1 << 4);
  749. break;
  750. case SNPS_28NM_INTEGRATED_PHY:
  751. chg_det = ulpi_read(phy, 0x87);
  752. ret = chg_det & 1;
  753. break;
  754. default:
  755. break;
  756. }
  757. return ret;
  758. }
  759. static void msm_chg_enable_primary_det(struct msm_otg *motg)
  760. {
  761. struct usb_phy *phy = &motg->phy;
  762. u32 chg_det;
  763. switch (motg->pdata->phy_type) {
  764. case CI_45NM_INTEGRATED_PHY:
  765. chg_det = ulpi_read(phy, 0x34);
  766. /* enable chg detection */
  767. chg_det &= ~(1 << 0);
  768. ulpi_write(phy, chg_det, 0x34);
  769. break;
  770. case SNPS_28NM_INTEGRATED_PHY:
  771. /*
  772. * Configure DP as current source, DM as current sink
  773. * and enable battery charging comparators.
  774. */
  775. ulpi_write(phy, 0x2, 0x85);
  776. ulpi_write(phy, 0x1, 0x85);
  777. break;
  778. default:
  779. break;
  780. }
  781. }
  782. static bool msm_chg_check_dcd(struct msm_otg *motg)
  783. {
  784. struct usb_phy *phy = &motg->phy;
  785. u32 line_state;
  786. bool ret = false;
  787. switch (motg->pdata->phy_type) {
  788. case CI_45NM_INTEGRATED_PHY:
  789. line_state = ulpi_read(phy, 0x15);
  790. ret = !(line_state & 1);
  791. break;
  792. case SNPS_28NM_INTEGRATED_PHY:
  793. line_state = ulpi_read(phy, 0x87);
  794. ret = line_state & 2;
  795. break;
  796. default:
  797. break;
  798. }
  799. return ret;
  800. }
  801. static void msm_chg_disable_dcd(struct msm_otg *motg)
  802. {
  803. struct usb_phy *phy = &motg->phy;
  804. u32 chg_det;
  805. switch (motg->pdata->phy_type) {
  806. case CI_45NM_INTEGRATED_PHY:
  807. chg_det = ulpi_read(phy, 0x34);
  808. chg_det &= ~(1 << 5);
  809. ulpi_write(phy, chg_det, 0x34);
  810. break;
  811. case SNPS_28NM_INTEGRATED_PHY:
  812. ulpi_write(phy, 0x10, 0x86);
  813. break;
  814. default:
  815. break;
  816. }
  817. }
  818. static void msm_chg_enable_dcd(struct msm_otg *motg)
  819. {
  820. struct usb_phy *phy = &motg->phy;
  821. u32 chg_det;
  822. switch (motg->pdata->phy_type) {
  823. case CI_45NM_INTEGRATED_PHY:
  824. chg_det = ulpi_read(phy, 0x34);
  825. /* Turn on D+ current source */
  826. chg_det |= (1 << 5);
  827. ulpi_write(phy, chg_det, 0x34);
  828. break;
  829. case SNPS_28NM_INTEGRATED_PHY:
  830. /* Data contact detection enable */
  831. ulpi_write(phy, 0x10, 0x85);
  832. break;
  833. default:
  834. break;
  835. }
  836. }
  837. static void msm_chg_block_on(struct msm_otg *motg)
  838. {
  839. struct usb_phy *phy = &motg->phy;
  840. u32 func_ctrl, chg_det;
  841. /* put the controller in non-driving mode */
  842. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  843. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  844. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  845. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  846. switch (motg->pdata->phy_type) {
  847. case CI_45NM_INTEGRATED_PHY:
  848. chg_det = ulpi_read(phy, 0x34);
  849. /* control chg block via ULPI */
  850. chg_det &= ~(1 << 3);
  851. ulpi_write(phy, chg_det, 0x34);
  852. /* Turn on chg detect block */
  853. chg_det &= ~(1 << 1);
  854. ulpi_write(phy, chg_det, 0x34);
  855. udelay(20);
  856. break;
  857. case SNPS_28NM_INTEGRATED_PHY:
  858. /* Clear charger detecting control bits */
  859. ulpi_write(phy, 0x3F, 0x86);
  860. /* Clear alt interrupt latch and enable bits */
  861. ulpi_write(phy, 0x1F, 0x92);
  862. ulpi_write(phy, 0x1F, 0x95);
  863. udelay(100);
  864. break;
  865. default:
  866. break;
  867. }
  868. }
  869. static void msm_chg_block_off(struct msm_otg *motg)
  870. {
  871. struct usb_phy *phy = &motg->phy;
  872. u32 func_ctrl, chg_det;
  873. switch (motg->pdata->phy_type) {
  874. case CI_45NM_INTEGRATED_PHY:
  875. chg_det = ulpi_read(phy, 0x34);
  876. /* Turn off charger block */
  877. chg_det |= ~(1 << 1);
  878. ulpi_write(phy, chg_det, 0x34);
  879. break;
  880. case SNPS_28NM_INTEGRATED_PHY:
  881. /* Clear charger detecting control bits */
  882. ulpi_write(phy, 0x3F, 0x86);
  883. /* Clear alt interrupt latch and enable bits */
  884. ulpi_write(phy, 0x1F, 0x92);
  885. ulpi_write(phy, 0x1F, 0x95);
  886. break;
  887. default:
  888. break;
  889. }
  890. /* put the controller in normal mode */
  891. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  892. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  893. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  894. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  895. }
  896. #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
  897. #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
  898. #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
  899. #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
  900. static void msm_chg_detect_work(struct work_struct *w)
  901. {
  902. struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
  903. struct usb_phy *phy = &motg->phy;
  904. bool is_dcd, tmout, vout;
  905. unsigned long delay;
  906. dev_dbg(phy->dev, "chg detection work\n");
  907. switch (motg->chg_state) {
  908. case USB_CHG_STATE_UNDEFINED:
  909. pm_runtime_get_sync(phy->dev);
  910. msm_chg_block_on(motg);
  911. msm_chg_enable_dcd(motg);
  912. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  913. motg->dcd_retries = 0;
  914. delay = MSM_CHG_DCD_POLL_TIME;
  915. break;
  916. case USB_CHG_STATE_WAIT_FOR_DCD:
  917. is_dcd = msm_chg_check_dcd(motg);
  918. tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
  919. if (is_dcd || tmout) {
  920. msm_chg_disable_dcd(motg);
  921. msm_chg_enable_primary_det(motg);
  922. delay = MSM_CHG_PRIMARY_DET_TIME;
  923. motg->chg_state = USB_CHG_STATE_DCD_DONE;
  924. } else {
  925. delay = MSM_CHG_DCD_POLL_TIME;
  926. }
  927. break;
  928. case USB_CHG_STATE_DCD_DONE:
  929. vout = msm_chg_check_primary_det(motg);
  930. if (vout) {
  931. msm_chg_enable_secondary_det(motg);
  932. delay = MSM_CHG_SECONDARY_DET_TIME;
  933. motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  934. } else {
  935. motg->chg_type = USB_SDP_CHARGER;
  936. motg->chg_state = USB_CHG_STATE_DETECTED;
  937. delay = 0;
  938. }
  939. break;
  940. case USB_CHG_STATE_PRIMARY_DONE:
  941. vout = msm_chg_check_secondary_det(motg);
  942. if (vout)
  943. motg->chg_type = USB_DCP_CHARGER;
  944. else
  945. motg->chg_type = USB_CDP_CHARGER;
  946. motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
  947. /* fall through */
  948. case USB_CHG_STATE_SECONDARY_DONE:
  949. motg->chg_state = USB_CHG_STATE_DETECTED;
  950. case USB_CHG_STATE_DETECTED:
  951. msm_chg_block_off(motg);
  952. dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
  953. schedule_work(&motg->sm_work);
  954. return;
  955. default:
  956. return;
  957. }
  958. schedule_delayed_work(&motg->chg_work, delay);
  959. }
  960. /*
  961. * We support OTG, Peripheral only and Host only configurations. In case
  962. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  963. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  964. * enabled when switch is controlled by user and default mode is supplied
  965. * by board file, which can be changed by userspace later.
  966. */
  967. static void msm_otg_init_sm(struct msm_otg *motg)
  968. {
  969. struct msm_otg_platform_data *pdata = motg->pdata;
  970. u32 otgsc = readl(USB_OTGSC);
  971. switch (pdata->mode) {
  972. case USB_DR_MODE_OTG:
  973. if (pdata->otg_control == OTG_PHY_CONTROL) {
  974. if (otgsc & OTGSC_ID)
  975. set_bit(ID, &motg->inputs);
  976. else
  977. clear_bit(ID, &motg->inputs);
  978. if (otgsc & OTGSC_BSV)
  979. set_bit(B_SESS_VLD, &motg->inputs);
  980. else
  981. clear_bit(B_SESS_VLD, &motg->inputs);
  982. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  983. set_bit(ID, &motg->inputs);
  984. clear_bit(B_SESS_VLD, &motg->inputs);
  985. }
  986. break;
  987. case USB_DR_MODE_HOST:
  988. clear_bit(ID, &motg->inputs);
  989. break;
  990. case USB_DR_MODE_PERIPHERAL:
  991. set_bit(ID, &motg->inputs);
  992. if (otgsc & OTGSC_BSV)
  993. set_bit(B_SESS_VLD, &motg->inputs);
  994. else
  995. clear_bit(B_SESS_VLD, &motg->inputs);
  996. break;
  997. default:
  998. break;
  999. }
  1000. }
  1001. static void msm_otg_sm_work(struct work_struct *w)
  1002. {
  1003. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  1004. struct usb_otg *otg = motg->phy.otg;
  1005. switch (otg->phy->state) {
  1006. case OTG_STATE_UNDEFINED:
  1007. dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
  1008. msm_otg_reset(otg->phy);
  1009. msm_otg_init_sm(motg);
  1010. otg->phy->state = OTG_STATE_B_IDLE;
  1011. /* FALL THROUGH */
  1012. case OTG_STATE_B_IDLE:
  1013. dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n");
  1014. if (!test_bit(ID, &motg->inputs) && otg->host) {
  1015. /* disable BSV bit */
  1016. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  1017. msm_otg_start_host(otg->phy, 1);
  1018. otg->phy->state = OTG_STATE_A_HOST;
  1019. } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1020. switch (motg->chg_state) {
  1021. case USB_CHG_STATE_UNDEFINED:
  1022. msm_chg_detect_work(&motg->chg_work.work);
  1023. break;
  1024. case USB_CHG_STATE_DETECTED:
  1025. switch (motg->chg_type) {
  1026. case USB_DCP_CHARGER:
  1027. msm_otg_notify_charger(motg,
  1028. IDEV_CHG_MAX);
  1029. break;
  1030. case USB_CDP_CHARGER:
  1031. msm_otg_notify_charger(motg,
  1032. IDEV_CHG_MAX);
  1033. msm_otg_start_peripheral(otg->phy, 1);
  1034. otg->phy->state
  1035. = OTG_STATE_B_PERIPHERAL;
  1036. break;
  1037. case USB_SDP_CHARGER:
  1038. msm_otg_notify_charger(motg, IUNIT);
  1039. msm_otg_start_peripheral(otg->phy, 1);
  1040. otg->phy->state
  1041. = OTG_STATE_B_PERIPHERAL;
  1042. break;
  1043. default:
  1044. break;
  1045. }
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. } else {
  1051. /*
  1052. * If charger detection work is pending, decrement
  1053. * the pm usage counter to balance with the one that
  1054. * is incremented in charger detection work.
  1055. */
  1056. if (cancel_delayed_work_sync(&motg->chg_work)) {
  1057. pm_runtime_put_sync(otg->phy->dev);
  1058. msm_otg_reset(otg->phy);
  1059. }
  1060. msm_otg_notify_charger(motg, 0);
  1061. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1062. motg->chg_type = USB_INVALID_CHARGER;
  1063. }
  1064. if (otg->phy->state == OTG_STATE_B_IDLE)
  1065. pm_runtime_put_sync(otg->phy->dev);
  1066. break;
  1067. case OTG_STATE_B_PERIPHERAL:
  1068. dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
  1069. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  1070. !test_bit(ID, &motg->inputs)) {
  1071. msm_otg_notify_charger(motg, 0);
  1072. msm_otg_start_peripheral(otg->phy, 0);
  1073. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1074. motg->chg_type = USB_INVALID_CHARGER;
  1075. otg->phy->state = OTG_STATE_B_IDLE;
  1076. msm_otg_reset(otg->phy);
  1077. schedule_work(w);
  1078. }
  1079. break;
  1080. case OTG_STATE_A_HOST:
  1081. dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
  1082. if (test_bit(ID, &motg->inputs)) {
  1083. msm_otg_start_host(otg->phy, 0);
  1084. otg->phy->state = OTG_STATE_B_IDLE;
  1085. msm_otg_reset(otg->phy);
  1086. schedule_work(w);
  1087. }
  1088. break;
  1089. default:
  1090. break;
  1091. }
  1092. }
  1093. static irqreturn_t msm_otg_irq(int irq, void *data)
  1094. {
  1095. struct msm_otg *motg = data;
  1096. struct usb_phy *phy = &motg->phy;
  1097. u32 otgsc = 0;
  1098. if (atomic_read(&motg->in_lpm)) {
  1099. disable_irq_nosync(irq);
  1100. motg->async_int = 1;
  1101. pm_runtime_get(phy->dev);
  1102. return IRQ_HANDLED;
  1103. }
  1104. otgsc = readl(USB_OTGSC);
  1105. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  1106. return IRQ_NONE;
  1107. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  1108. if (otgsc & OTGSC_ID)
  1109. set_bit(ID, &motg->inputs);
  1110. else
  1111. clear_bit(ID, &motg->inputs);
  1112. dev_dbg(phy->dev, "ID set/clear\n");
  1113. pm_runtime_get_noresume(phy->dev);
  1114. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  1115. if (otgsc & OTGSC_BSV)
  1116. set_bit(B_SESS_VLD, &motg->inputs);
  1117. else
  1118. clear_bit(B_SESS_VLD, &motg->inputs);
  1119. dev_dbg(phy->dev, "BSV set/clear\n");
  1120. pm_runtime_get_noresume(phy->dev);
  1121. }
  1122. writel(otgsc, USB_OTGSC);
  1123. schedule_work(&motg->sm_work);
  1124. return IRQ_HANDLED;
  1125. }
  1126. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  1127. {
  1128. struct msm_otg *motg = s->private;
  1129. struct usb_otg *otg = motg->phy.otg;
  1130. switch (otg->phy->state) {
  1131. case OTG_STATE_A_HOST:
  1132. seq_puts(s, "host\n");
  1133. break;
  1134. case OTG_STATE_B_PERIPHERAL:
  1135. seq_puts(s, "peripheral\n");
  1136. break;
  1137. default:
  1138. seq_puts(s, "none\n");
  1139. break;
  1140. }
  1141. return 0;
  1142. }
  1143. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  1144. {
  1145. return single_open(file, msm_otg_mode_show, inode->i_private);
  1146. }
  1147. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  1148. size_t count, loff_t *ppos)
  1149. {
  1150. struct seq_file *s = file->private_data;
  1151. struct msm_otg *motg = s->private;
  1152. char buf[16];
  1153. struct usb_otg *otg = motg->phy.otg;
  1154. int status = count;
  1155. enum usb_dr_mode req_mode;
  1156. memset(buf, 0x00, sizeof(buf));
  1157. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  1158. status = -EFAULT;
  1159. goto out;
  1160. }
  1161. if (!strncmp(buf, "host", 4)) {
  1162. req_mode = USB_DR_MODE_HOST;
  1163. } else if (!strncmp(buf, "peripheral", 10)) {
  1164. req_mode = USB_DR_MODE_PERIPHERAL;
  1165. } else if (!strncmp(buf, "none", 4)) {
  1166. req_mode = USB_DR_MODE_UNKNOWN;
  1167. } else {
  1168. status = -EINVAL;
  1169. goto out;
  1170. }
  1171. switch (req_mode) {
  1172. case USB_DR_MODE_UNKNOWN:
  1173. switch (otg->phy->state) {
  1174. case OTG_STATE_A_HOST:
  1175. case OTG_STATE_B_PERIPHERAL:
  1176. set_bit(ID, &motg->inputs);
  1177. clear_bit(B_SESS_VLD, &motg->inputs);
  1178. break;
  1179. default:
  1180. goto out;
  1181. }
  1182. break;
  1183. case USB_DR_MODE_PERIPHERAL:
  1184. switch (otg->phy->state) {
  1185. case OTG_STATE_B_IDLE:
  1186. case OTG_STATE_A_HOST:
  1187. set_bit(ID, &motg->inputs);
  1188. set_bit(B_SESS_VLD, &motg->inputs);
  1189. break;
  1190. default:
  1191. goto out;
  1192. }
  1193. break;
  1194. case USB_DR_MODE_HOST:
  1195. switch (otg->phy->state) {
  1196. case OTG_STATE_B_IDLE:
  1197. case OTG_STATE_B_PERIPHERAL:
  1198. clear_bit(ID, &motg->inputs);
  1199. break;
  1200. default:
  1201. goto out;
  1202. }
  1203. break;
  1204. default:
  1205. goto out;
  1206. }
  1207. pm_runtime_get_sync(otg->phy->dev);
  1208. schedule_work(&motg->sm_work);
  1209. out:
  1210. return status;
  1211. }
  1212. static const struct file_operations msm_otg_mode_fops = {
  1213. .open = msm_otg_mode_open,
  1214. .read = seq_read,
  1215. .write = msm_otg_mode_write,
  1216. .llseek = seq_lseek,
  1217. .release = single_release,
  1218. };
  1219. static struct dentry *msm_otg_dbg_root;
  1220. static struct dentry *msm_otg_dbg_mode;
  1221. static int msm_otg_debugfs_init(struct msm_otg *motg)
  1222. {
  1223. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  1224. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  1225. return -ENODEV;
  1226. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  1227. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  1228. if (!msm_otg_dbg_mode) {
  1229. debugfs_remove(msm_otg_dbg_root);
  1230. msm_otg_dbg_root = NULL;
  1231. return -ENODEV;
  1232. }
  1233. return 0;
  1234. }
  1235. static void msm_otg_debugfs_cleanup(void)
  1236. {
  1237. debugfs_remove(msm_otg_dbg_mode);
  1238. debugfs_remove(msm_otg_dbg_root);
  1239. }
  1240. static const struct of_device_id msm_otg_dt_match[] = {
  1241. {
  1242. .compatible = "qcom,usb-otg-ci",
  1243. .data = (void *) CI_45NM_INTEGRATED_PHY
  1244. },
  1245. {
  1246. .compatible = "qcom,usb-otg-snps",
  1247. .data = (void *) SNPS_28NM_INTEGRATED_PHY
  1248. },
  1249. { }
  1250. };
  1251. MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
  1252. static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
  1253. {
  1254. struct msm_otg_platform_data *pdata;
  1255. const struct of_device_id *id;
  1256. struct device_node *node = pdev->dev.of_node;
  1257. struct property *prop;
  1258. int len, ret, words;
  1259. u32 val, tmp[3];
  1260. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1261. if (!pdata)
  1262. return -ENOMEM;
  1263. motg->pdata = pdata;
  1264. id = of_match_device(msm_otg_dt_match, &pdev->dev);
  1265. pdata->phy_type = (enum msm_usb_phy_type) id->data;
  1266. motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
  1267. if (IS_ERR(motg->link_rst))
  1268. return PTR_ERR(motg->link_rst);
  1269. motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
  1270. if (IS_ERR(motg->phy_rst))
  1271. motg->phy_rst = NULL;
  1272. pdata->mode = of_usb_get_dr_mode(node);
  1273. if (pdata->mode == USB_DR_MODE_UNKNOWN)
  1274. pdata->mode = USB_DR_MODE_OTG;
  1275. pdata->otg_control = OTG_PHY_CONTROL;
  1276. if (!of_property_read_u32(node, "qcom,otg-control", &val))
  1277. if (val == OTG_PMIC_CONTROL)
  1278. pdata->otg_control = val;
  1279. if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
  1280. motg->phy_number = val;
  1281. motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
  1282. motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
  1283. motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
  1284. if (of_get_property(node, "qcom,vdd-levels", &len) &&
  1285. len == sizeof(tmp)) {
  1286. of_property_read_u32_array(node, "qcom,vdd-levels",
  1287. tmp, len / sizeof(*tmp));
  1288. motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
  1289. motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
  1290. motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
  1291. }
  1292. prop = of_find_property(node, "qcom,phy-init-sequence", &len);
  1293. if (!prop || !len)
  1294. return 0;
  1295. words = len / sizeof(u32);
  1296. if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
  1297. dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
  1298. return 0;
  1299. }
  1300. pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  1301. if (!pdata->phy_init_seq) {
  1302. dev_warn(&pdev->dev, "No space for PHY init sequence\n");
  1303. return 0;
  1304. }
  1305. ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
  1306. pdata->phy_init_seq, words);
  1307. if (!ret)
  1308. pdata->phy_init_sz = words;
  1309. return 0;
  1310. }
  1311. static int msm_otg_probe(struct platform_device *pdev)
  1312. {
  1313. struct regulator_bulk_data regs[3];
  1314. int ret = 0;
  1315. struct device_node *np = pdev->dev.of_node;
  1316. struct msm_otg_platform_data *pdata;
  1317. struct resource *res;
  1318. struct msm_otg *motg;
  1319. struct usb_phy *phy;
  1320. void __iomem *phy_select;
  1321. motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
  1322. if (!motg) {
  1323. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  1324. return -ENOMEM;
  1325. }
  1326. pdata = dev_get_platdata(&pdev->dev);
  1327. if (!pdata) {
  1328. if (!np)
  1329. return -ENXIO;
  1330. ret = msm_otg_read_dt(pdev, motg);
  1331. if (ret)
  1332. return ret;
  1333. }
  1334. motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  1335. GFP_KERNEL);
  1336. if (!motg->phy.otg) {
  1337. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  1338. return -ENOMEM;
  1339. }
  1340. phy = &motg->phy;
  1341. phy->dev = &pdev->dev;
  1342. if (motg->pdata->phy_clk_reset) {
  1343. motg->phy_reset_clk = devm_clk_get(&pdev->dev,
  1344. np ? "phy" : "usb_phy_clk");
  1345. if (IS_ERR(motg->phy_reset_clk)) {
  1346. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  1347. return PTR_ERR(motg->phy_reset_clk);
  1348. }
  1349. }
  1350. motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
  1351. if (IS_ERR(motg->clk)) {
  1352. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  1353. return PTR_ERR(motg->clk);
  1354. }
  1355. /*
  1356. * If USB Core is running its protocol engine based on CORE CLK,
  1357. * CORE CLK must be running at >55Mhz for correct HSUSB
  1358. * operation and USB core cannot tolerate frequency changes on
  1359. * CORE CLK.
  1360. */
  1361. motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
  1362. if (IS_ERR(motg->pclk)) {
  1363. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  1364. return PTR_ERR(motg->pclk);
  1365. }
  1366. /*
  1367. * USB core clock is not present on all MSM chips. This
  1368. * clock is introduced to remove the dependency on AXI
  1369. * bus frequency.
  1370. */
  1371. motg->core_clk = devm_clk_get(&pdev->dev,
  1372. np ? "alt_core" : "usb_hs_core_clk");
  1373. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1374. if (!res)
  1375. return -EINVAL;
  1376. motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  1377. if (!motg->regs)
  1378. return -ENOMEM;
  1379. /*
  1380. * NOTE: The PHYs can be multiplexed between the chipidea controller
  1381. * and the dwc3 controller, using a single bit. It is important that
  1382. * the dwc3 driver does not set this bit in an incompatible way.
  1383. */
  1384. if (motg->phy_number) {
  1385. phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
  1386. if (!phy_select)
  1387. return -ENOMEM;
  1388. /* Enable second PHY with the OTG port */
  1389. writel(0x1, phy_select);
  1390. }
  1391. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  1392. motg->irq = platform_get_irq(pdev, 0);
  1393. if (motg->irq < 0) {
  1394. dev_err(&pdev->dev, "platform_get_irq failed\n");
  1395. return motg->irq;
  1396. }
  1397. regs[0].supply = "vddcx";
  1398. regs[1].supply = "v3p3";
  1399. regs[2].supply = "v1p8";
  1400. ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
  1401. if (ret)
  1402. return ret;
  1403. motg->vddcx = regs[0].consumer;
  1404. motg->v3p3 = regs[1].consumer;
  1405. motg->v1p8 = regs[2].consumer;
  1406. clk_set_rate(motg->clk, 60000000);
  1407. clk_prepare_enable(motg->clk);
  1408. clk_prepare_enable(motg->pclk);
  1409. if (!IS_ERR(motg->core_clk))
  1410. clk_prepare_enable(motg->core_clk);
  1411. ret = msm_hsusb_init_vddcx(motg, 1);
  1412. if (ret) {
  1413. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  1414. goto disable_clks;
  1415. }
  1416. ret = msm_hsusb_ldo_init(motg, 1);
  1417. if (ret) {
  1418. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  1419. goto disable_vddcx;
  1420. }
  1421. ret = msm_hsusb_ldo_set_mode(motg, 1);
  1422. if (ret) {
  1423. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  1424. goto disable_ldo;
  1425. }
  1426. writel(0, USB_USBINTR);
  1427. writel(0, USB_OTGSC);
  1428. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  1429. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  1430. ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
  1431. "msm_otg", motg);
  1432. if (ret) {
  1433. dev_err(&pdev->dev, "request irq failed\n");
  1434. goto disable_ldo;
  1435. }
  1436. phy->init = msm_phy_init;
  1437. phy->set_power = msm_otg_set_power;
  1438. phy->notify_disconnect = msm_phy_notify_disconnect;
  1439. phy->type = USB_PHY_TYPE_USB2;
  1440. phy->io_ops = &msm_otg_io_ops;
  1441. phy->otg->phy = &motg->phy;
  1442. phy->otg->set_host = msm_otg_set_host;
  1443. phy->otg->set_peripheral = msm_otg_set_peripheral;
  1444. msm_usb_reset(phy);
  1445. ret = usb_add_phy_dev(&motg->phy);
  1446. if (ret) {
  1447. dev_err(&pdev->dev, "usb_add_phy failed\n");
  1448. goto disable_ldo;
  1449. }
  1450. platform_set_drvdata(pdev, motg);
  1451. device_init_wakeup(&pdev->dev, 1);
  1452. if (motg->pdata->mode == USB_DR_MODE_OTG &&
  1453. motg->pdata->otg_control == OTG_USER_CONTROL) {
  1454. ret = msm_otg_debugfs_init(motg);
  1455. if (ret)
  1456. dev_dbg(&pdev->dev, "Can not create mode change file\n");
  1457. }
  1458. pm_runtime_set_active(&pdev->dev);
  1459. pm_runtime_enable(&pdev->dev);
  1460. return 0;
  1461. disable_ldo:
  1462. msm_hsusb_ldo_init(motg, 0);
  1463. disable_vddcx:
  1464. msm_hsusb_init_vddcx(motg, 0);
  1465. disable_clks:
  1466. clk_disable_unprepare(motg->pclk);
  1467. clk_disable_unprepare(motg->clk);
  1468. if (!IS_ERR(motg->core_clk))
  1469. clk_disable_unprepare(motg->core_clk);
  1470. return ret;
  1471. }
  1472. static int msm_otg_remove(struct platform_device *pdev)
  1473. {
  1474. struct msm_otg *motg = platform_get_drvdata(pdev);
  1475. struct usb_phy *phy = &motg->phy;
  1476. int cnt = 0;
  1477. if (phy->otg->host || phy->otg->gadget)
  1478. return -EBUSY;
  1479. msm_otg_debugfs_cleanup();
  1480. cancel_delayed_work_sync(&motg->chg_work);
  1481. cancel_work_sync(&motg->sm_work);
  1482. pm_runtime_resume(&pdev->dev);
  1483. device_init_wakeup(&pdev->dev, 0);
  1484. pm_runtime_disable(&pdev->dev);
  1485. usb_remove_phy(phy);
  1486. disable_irq(motg->irq);
  1487. /*
  1488. * Put PHY in low power mode.
  1489. */
  1490. ulpi_read(phy, 0x14);
  1491. ulpi_write(phy, 0x08, 0x09);
  1492. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  1493. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  1494. if (readl(USB_PORTSC) & PORTSC_PHCD)
  1495. break;
  1496. udelay(1);
  1497. cnt++;
  1498. }
  1499. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  1500. dev_err(phy->dev, "Unable to suspend PHY\n");
  1501. clk_disable_unprepare(motg->pclk);
  1502. clk_disable_unprepare(motg->clk);
  1503. if (!IS_ERR(motg->core_clk))
  1504. clk_disable_unprepare(motg->core_clk);
  1505. msm_hsusb_ldo_init(motg, 0);
  1506. pm_runtime_set_suspended(&pdev->dev);
  1507. return 0;
  1508. }
  1509. #ifdef CONFIG_PM_RUNTIME
  1510. static int msm_otg_runtime_idle(struct device *dev)
  1511. {
  1512. struct msm_otg *motg = dev_get_drvdata(dev);
  1513. struct usb_otg *otg = motg->phy.otg;
  1514. dev_dbg(dev, "OTG runtime idle\n");
  1515. /*
  1516. * It is observed some times that a spurious interrupt
  1517. * comes when PHY is put into LPM immediately after PHY reset.
  1518. * This 1 sec delay also prevents entering into LPM immediately
  1519. * after asynchronous interrupt.
  1520. */
  1521. if (otg->phy->state != OTG_STATE_UNDEFINED)
  1522. pm_schedule_suspend(dev, 1000);
  1523. return -EAGAIN;
  1524. }
  1525. static int msm_otg_runtime_suspend(struct device *dev)
  1526. {
  1527. struct msm_otg *motg = dev_get_drvdata(dev);
  1528. dev_dbg(dev, "OTG runtime suspend\n");
  1529. return msm_otg_suspend(motg);
  1530. }
  1531. static int msm_otg_runtime_resume(struct device *dev)
  1532. {
  1533. struct msm_otg *motg = dev_get_drvdata(dev);
  1534. dev_dbg(dev, "OTG runtime resume\n");
  1535. return msm_otg_resume(motg);
  1536. }
  1537. #endif
  1538. #ifdef CONFIG_PM_SLEEP
  1539. static int msm_otg_pm_suspend(struct device *dev)
  1540. {
  1541. struct msm_otg *motg = dev_get_drvdata(dev);
  1542. dev_dbg(dev, "OTG PM suspend\n");
  1543. return msm_otg_suspend(motg);
  1544. }
  1545. static int msm_otg_pm_resume(struct device *dev)
  1546. {
  1547. struct msm_otg *motg = dev_get_drvdata(dev);
  1548. int ret;
  1549. dev_dbg(dev, "OTG PM resume\n");
  1550. ret = msm_otg_resume(motg);
  1551. if (ret)
  1552. return ret;
  1553. /*
  1554. * Runtime PM Documentation recommends bringing the
  1555. * device to full powered state upon resume.
  1556. */
  1557. pm_runtime_disable(dev);
  1558. pm_runtime_set_active(dev);
  1559. pm_runtime_enable(dev);
  1560. return 0;
  1561. }
  1562. #endif
  1563. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  1564. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  1565. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  1566. msm_otg_runtime_idle)
  1567. };
  1568. static struct platform_driver msm_otg_driver = {
  1569. .probe = msm_otg_probe,
  1570. .remove = msm_otg_remove,
  1571. .driver = {
  1572. .name = DRIVER_NAME,
  1573. .pm = &msm_otg_dev_pm_ops,
  1574. .of_match_table = msm_otg_dt_match,
  1575. },
  1576. };
  1577. module_platform_driver(msm_otg_driver);
  1578. MODULE_LICENSE("GPL v2");
  1579. MODULE_DESCRIPTION("MSM USB transceiver driver");