msm_otg.c 42 KB

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