phy-msm-usb.c 42 KB

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