phy-msm-usb.c 42 KB

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