msm_otg.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  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->phy.dev, "HSUSB_VDDCX");
  62. if (IS_ERR(hsusb_vddcx)) {
  63. dev_err(motg->phy.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->phy.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->phy.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->phy.dev, "unable to set the voltage "
  85. "for hsusb vddcx\n");
  86. ret = regulator_disable(hsusb_vddcx);
  87. if (ret)
  88. dev_err(motg->phy.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->phy.dev, "HSUSB_3p3");
  98. if (IS_ERR(hsusb_3p3)) {
  99. dev_err(motg->phy.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->phy.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->phy.dev, "unable to enable the hsusb 3p3\n");
  112. goto put_3p3;
  113. }
  114. hsusb_1p8 = regulator_get(motg->phy.dev, "HSUSB_1p8");
  115. if (IS_ERR(hsusb_1p8)) {
  116. dev_err(motg->phy.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->phy.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->phy.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 usb_phy *phy, u32 reg)
  208. {
  209. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  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(phy->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 usb_phy *phy, u32 val, u32 reg)
  229. {
  230. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  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(phy->dev, "ulpi_write: timeout\n");
  245. return -ETIMEDOUT;
  246. }
  247. return 0;
  248. }
  249. static struct usb_phy_io_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->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
  261. seq[0], seq[1]);
  262. ulpi_write(&motg->phy, 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->phy.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->phy.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->phy.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->phy.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->phy, 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->phy, 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->phy.dev, "phy_reset: success\n");
  336. return 0;
  337. }
  338. #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
  339. static int msm_otg_reset(struct usb_phy *phy)
  340. {
  341. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  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(phy->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(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
  378. ulpi_write(phy, 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 usb_phy *phy = &motg->phy;
  388. struct usb_bus *bus = phy->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(phy, 0x14);
  412. if (pdata->otg_control == OTG_PHY_CONTROL)
  413. ulpi_write(phy, 0x01, 0x30);
  414. ulpi_write(phy, 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(phy->dev, "Unable to suspend PHY\n");
  430. msm_otg_reset(phy);
  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(phy->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(phy->dev, "USB in low power mode\n");
  463. return 0;
  464. }
  465. static int msm_otg_resume(struct msm_otg *motg)
  466. {
  467. struct usb_phy *phy = &motg->phy;
  468. struct usb_bus *bus = phy->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(phy->dev, "Unable to resume USB."
  509. "Re-plugin the cable\n");
  510. msm_otg_reset(phy);
  511. }
  512. skip_phy_resume:
  513. if (device_may_wakeup(phy->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(phy->dev);
  521. enable_irq(motg->irq);
  522. }
  523. dev_info(phy->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->phy.dev, "Avail curr from USB = %u\n", mA);
  533. motg->cur_power = mA;
  534. }
  535. static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
  536. {
  537. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  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 usb_phy *phy, int on)
  550. {
  551. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  552. struct msm_otg_platform_data *pdata = motg->pdata;
  553. struct usb_hcd *hcd;
  554. if (!phy->otg->host)
  555. return;
  556. hcd = bus_to_hcd(phy->otg->host);
  557. if (on) {
  558. dev_dbg(phy->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(phy->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 usb_otg *otg, struct usb_bus *host)
  583. {
  584. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  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->phy->dev, "Host mode is not supported\n");
  592. return -ENODEV;
  593. }
  594. if (!host) {
  595. if (otg->phy->state == OTG_STATE_A_HOST) {
  596. pm_runtime_get_sync(otg->phy->dev);
  597. msm_otg_start_host(otg->phy, 0);
  598. otg->host = NULL;
  599. otg->phy->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->phy->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->phy->dev);
  616. schedule_work(&motg->sm_work);
  617. }
  618. return 0;
  619. }
  620. static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
  621. {
  622. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  623. struct msm_otg_platform_data *pdata = motg->pdata;
  624. if (!phy->otg->gadget)
  625. return;
  626. if (on) {
  627. dev_dbg(phy->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(phy->otg->gadget);
  636. } else {
  637. dev_dbg(phy->dev, "gadget off\n");
  638. usb_gadget_vbus_disconnect(phy->otg->gadget);
  639. if (pdata->setup_gpio)
  640. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  641. }
  642. }
  643. static int msm_otg_set_peripheral(struct usb_otg *otg,
  644. struct usb_gadget *gadget)
  645. {
  646. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  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->phy->dev, "Peripheral mode is not supported\n");
  653. return -ENODEV;
  654. }
  655. if (!gadget) {
  656. if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
  657. pm_runtime_get_sync(otg->phy->dev);
  658. msm_otg_start_peripheral(otg->phy, 0);
  659. otg->gadget = NULL;
  660. otg->phy->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->phy->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->phy->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 usb_phy *phy = &motg->phy;
  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(phy, 0x34);
  687. ret = chg_det & (1 << 4);
  688. break;
  689. case SNPS_28NM_INTEGRATED_PHY:
  690. chg_det = ulpi_read(phy, 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 usb_phy *phy = &motg->phy;
  701. u32 chg_det;
  702. switch (motg->pdata->phy_type) {
  703. case CI_45NM_INTEGRATED_PHY:
  704. chg_det = ulpi_read(phy, 0x34);
  705. /* Turn off charger block */
  706. chg_det |= ~(1 << 1);
  707. ulpi_write(phy, chg_det, 0x34);
  708. udelay(20);
  709. /* control chg block via ULPI */
  710. chg_det &= ~(1 << 3);
  711. ulpi_write(phy, chg_det, 0x34);
  712. /* put it in host mode for enabling D- source */
  713. chg_det &= ~(1 << 2);
  714. ulpi_write(phy, chg_det, 0x34);
  715. /* Turn on chg detect block */
  716. chg_det &= ~(1 << 1);
  717. ulpi_write(phy, chg_det, 0x34);
  718. udelay(20);
  719. /* enable chg detection */
  720. chg_det &= ~(1 << 0);
  721. ulpi_write(phy, 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(phy, 0x8, 0x85);
  729. ulpi_write(phy, 0x2, 0x85);
  730. ulpi_write(phy, 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 usb_phy *phy = &motg->phy;
  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(phy, 0x34);
  744. ret = chg_det & (1 << 4);
  745. break;
  746. case SNPS_28NM_INTEGRATED_PHY:
  747. chg_det = ulpi_read(phy, 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 usb_phy *phy = &motg->phy;
  758. u32 chg_det;
  759. switch (motg->pdata->phy_type) {
  760. case CI_45NM_INTEGRATED_PHY:
  761. chg_det = ulpi_read(phy, 0x34);
  762. /* enable chg detection */
  763. chg_det &= ~(1 << 0);
  764. ulpi_write(phy, 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(phy, 0x2, 0x85);
  772. ulpi_write(phy, 0x1, 0x85);
  773. break;
  774. default:
  775. break;
  776. }
  777. }
  778. static bool msm_chg_check_dcd(struct msm_otg *motg)
  779. {
  780. struct usb_phy *phy = &motg->phy;
  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(phy, 0x15);
  786. ret = !(line_state & 1);
  787. break;
  788. case SNPS_28NM_INTEGRATED_PHY:
  789. line_state = ulpi_read(phy, 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 usb_phy *phy = &motg->phy;
  800. u32 chg_det;
  801. switch (motg->pdata->phy_type) {
  802. case CI_45NM_INTEGRATED_PHY:
  803. chg_det = ulpi_read(phy, 0x34);
  804. chg_det &= ~(1 << 5);
  805. ulpi_write(phy, chg_det, 0x34);
  806. break;
  807. case SNPS_28NM_INTEGRATED_PHY:
  808. ulpi_write(phy, 0x10, 0x86);
  809. break;
  810. default:
  811. break;
  812. }
  813. }
  814. static void msm_chg_enable_dcd(struct msm_otg *motg)
  815. {
  816. struct usb_phy *phy = &motg->phy;
  817. u32 chg_det;
  818. switch (motg->pdata->phy_type) {
  819. case CI_45NM_INTEGRATED_PHY:
  820. chg_det = ulpi_read(phy, 0x34);
  821. /* Turn on D+ current source */
  822. chg_det |= (1 << 5);
  823. ulpi_write(phy, chg_det, 0x34);
  824. break;
  825. case SNPS_28NM_INTEGRATED_PHY:
  826. /* Data contact detection enable */
  827. ulpi_write(phy, 0x10, 0x85);
  828. break;
  829. default:
  830. break;
  831. }
  832. }
  833. static void msm_chg_block_on(struct msm_otg *motg)
  834. {
  835. struct usb_phy *phy = &motg->phy;
  836. u32 func_ctrl, chg_det;
  837. /* put the controller in non-driving mode */
  838. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  839. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  840. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  841. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  842. switch (motg->pdata->phy_type) {
  843. case CI_45NM_INTEGRATED_PHY:
  844. chg_det = ulpi_read(phy, 0x34);
  845. /* control chg block via ULPI */
  846. chg_det &= ~(1 << 3);
  847. ulpi_write(phy, chg_det, 0x34);
  848. /* Turn on chg detect block */
  849. chg_det &= ~(1 << 1);
  850. ulpi_write(phy, chg_det, 0x34);
  851. udelay(20);
  852. break;
  853. case SNPS_28NM_INTEGRATED_PHY:
  854. /* Clear charger detecting control bits */
  855. ulpi_write(phy, 0x3F, 0x86);
  856. /* Clear alt interrupt latch and enable bits */
  857. ulpi_write(phy, 0x1F, 0x92);
  858. ulpi_write(phy, 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 usb_phy *phy = &motg->phy;
  868. u32 func_ctrl, chg_det;
  869. switch (motg->pdata->phy_type) {
  870. case CI_45NM_INTEGRATED_PHY:
  871. chg_det = ulpi_read(phy, 0x34);
  872. /* Turn off charger block */
  873. chg_det |= ~(1 << 1);
  874. ulpi_write(phy, chg_det, 0x34);
  875. break;
  876. case SNPS_28NM_INTEGRATED_PHY:
  877. /* Clear charger detecting control bits */
  878. ulpi_write(phy, 0x3F, 0x86);
  879. /* Clear alt interrupt latch and enable bits */
  880. ulpi_write(phy, 0x1F, 0x92);
  881. ulpi_write(phy, 0x1F, 0x95);
  882. break;
  883. default:
  884. break;
  885. }
  886. /* put the controller in normal mode */
  887. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  888. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  889. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  890. ulpi_write(phy, 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 usb_phy *phy = &motg->phy;
  900. bool is_dcd, tmout, vout;
  901. unsigned long delay;
  902. dev_dbg(phy->dev, "chg detection work\n");
  903. switch (motg->chg_state) {
  904. case USB_CHG_STATE_UNDEFINED:
  905. pm_runtime_get_sync(phy->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(phy->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 usb_otg *otg = motg->phy.otg;
  1008. switch (otg->phy->state) {
  1009. case OTG_STATE_UNDEFINED:
  1010. dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
  1011. msm_otg_reset(otg->phy);
  1012. msm_otg_init_sm(motg);
  1013. otg->phy->state = OTG_STATE_B_IDLE;
  1014. /* FALL THROUGH */
  1015. case OTG_STATE_B_IDLE:
  1016. dev_dbg(otg->phy->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->phy, 1);
  1021. otg->phy->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->phy, 1);
  1037. otg->phy->state
  1038. = OTG_STATE_B_PERIPHERAL;
  1039. break;
  1040. case USB_SDP_CHARGER:
  1041. msm_otg_notify_charger(motg, IUNIT);
  1042. msm_otg_start_peripheral(otg->phy, 1);
  1043. otg->phy->state
  1044. = OTG_STATE_B_PERIPHERAL;
  1045. break;
  1046. default:
  1047. break;
  1048. }
  1049. break;
  1050. default:
  1051. break;
  1052. }
  1053. } else {
  1054. /*
  1055. * If charger detection work is pending, decrement
  1056. * the pm usage counter to balance with the one that
  1057. * is incremented in charger detection work.
  1058. */
  1059. if (cancel_delayed_work_sync(&motg->chg_work)) {
  1060. pm_runtime_put_sync(otg->phy->dev);
  1061. msm_otg_reset(otg->phy);
  1062. }
  1063. msm_otg_notify_charger(motg, 0);
  1064. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1065. motg->chg_type = USB_INVALID_CHARGER;
  1066. }
  1067. pm_runtime_put_sync(otg->phy->dev);
  1068. break;
  1069. case OTG_STATE_B_PERIPHERAL:
  1070. dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
  1071. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  1072. !test_bit(ID, &motg->inputs)) {
  1073. msm_otg_notify_charger(motg, 0);
  1074. msm_otg_start_peripheral(otg->phy, 0);
  1075. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1076. motg->chg_type = USB_INVALID_CHARGER;
  1077. otg->phy->state = OTG_STATE_B_IDLE;
  1078. msm_otg_reset(otg->phy);
  1079. schedule_work(w);
  1080. }
  1081. break;
  1082. case OTG_STATE_A_HOST:
  1083. dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
  1084. if (test_bit(ID, &motg->inputs)) {
  1085. msm_otg_start_host(otg->phy, 0);
  1086. otg->phy->state = OTG_STATE_B_IDLE;
  1087. msm_otg_reset(otg->phy);
  1088. schedule_work(w);
  1089. }
  1090. break;
  1091. default:
  1092. break;
  1093. }
  1094. }
  1095. static irqreturn_t msm_otg_irq(int irq, void *data)
  1096. {
  1097. struct msm_otg *motg = data;
  1098. struct usb_phy *phy = &motg->phy;
  1099. u32 otgsc = 0;
  1100. if (atomic_read(&motg->in_lpm)) {
  1101. disable_irq_nosync(irq);
  1102. motg->async_int = 1;
  1103. pm_runtime_get(phy->dev);
  1104. return IRQ_HANDLED;
  1105. }
  1106. otgsc = readl(USB_OTGSC);
  1107. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  1108. return IRQ_NONE;
  1109. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  1110. if (otgsc & OTGSC_ID)
  1111. set_bit(ID, &motg->inputs);
  1112. else
  1113. clear_bit(ID, &motg->inputs);
  1114. dev_dbg(phy->dev, "ID set/clear\n");
  1115. pm_runtime_get_noresume(phy->dev);
  1116. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  1117. if (otgsc & OTGSC_BSV)
  1118. set_bit(B_SESS_VLD, &motg->inputs);
  1119. else
  1120. clear_bit(B_SESS_VLD, &motg->inputs);
  1121. dev_dbg(phy->dev, "BSV set/clear\n");
  1122. pm_runtime_get_noresume(phy->dev);
  1123. }
  1124. writel(otgsc, USB_OTGSC);
  1125. schedule_work(&motg->sm_work);
  1126. return IRQ_HANDLED;
  1127. }
  1128. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  1129. {
  1130. struct msm_otg *motg = s->private;
  1131. struct usb_otg *otg = motg->phy.otg;
  1132. switch (otg->phy->state) {
  1133. case OTG_STATE_A_HOST:
  1134. seq_printf(s, "host\n");
  1135. break;
  1136. case OTG_STATE_B_PERIPHERAL:
  1137. seq_printf(s, "peripheral\n");
  1138. break;
  1139. default:
  1140. seq_printf(s, "none\n");
  1141. break;
  1142. }
  1143. return 0;
  1144. }
  1145. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  1146. {
  1147. return single_open(file, msm_otg_mode_show, inode->i_private);
  1148. }
  1149. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  1150. size_t count, loff_t *ppos)
  1151. {
  1152. struct seq_file *s = file->private_data;
  1153. struct msm_otg *motg = s->private;
  1154. char buf[16];
  1155. struct usb_otg *otg = motg->phy.otg;
  1156. int status = count;
  1157. enum usb_mode_type req_mode;
  1158. memset(buf, 0x00, sizeof(buf));
  1159. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  1160. status = -EFAULT;
  1161. goto out;
  1162. }
  1163. if (!strncmp(buf, "host", 4)) {
  1164. req_mode = USB_HOST;
  1165. } else if (!strncmp(buf, "peripheral", 10)) {
  1166. req_mode = USB_PERIPHERAL;
  1167. } else if (!strncmp(buf, "none", 4)) {
  1168. req_mode = USB_NONE;
  1169. } else {
  1170. status = -EINVAL;
  1171. goto out;
  1172. }
  1173. switch (req_mode) {
  1174. case USB_NONE:
  1175. switch (otg->phy->state) {
  1176. case OTG_STATE_A_HOST:
  1177. case OTG_STATE_B_PERIPHERAL:
  1178. set_bit(ID, &motg->inputs);
  1179. clear_bit(B_SESS_VLD, &motg->inputs);
  1180. break;
  1181. default:
  1182. goto out;
  1183. }
  1184. break;
  1185. case USB_PERIPHERAL:
  1186. switch (otg->phy->state) {
  1187. case OTG_STATE_B_IDLE:
  1188. case OTG_STATE_A_HOST:
  1189. set_bit(ID, &motg->inputs);
  1190. set_bit(B_SESS_VLD, &motg->inputs);
  1191. break;
  1192. default:
  1193. goto out;
  1194. }
  1195. break;
  1196. case USB_HOST:
  1197. switch (otg->phy->state) {
  1198. case OTG_STATE_B_IDLE:
  1199. case OTG_STATE_B_PERIPHERAL:
  1200. clear_bit(ID, &motg->inputs);
  1201. break;
  1202. default:
  1203. goto out;
  1204. }
  1205. break;
  1206. default:
  1207. goto out;
  1208. }
  1209. pm_runtime_get_sync(otg->phy->dev);
  1210. schedule_work(&motg->sm_work);
  1211. out:
  1212. return status;
  1213. }
  1214. const struct file_operations msm_otg_mode_fops = {
  1215. .open = msm_otg_mode_open,
  1216. .read = seq_read,
  1217. .write = msm_otg_mode_write,
  1218. .llseek = seq_lseek,
  1219. .release = single_release,
  1220. };
  1221. static struct dentry *msm_otg_dbg_root;
  1222. static struct dentry *msm_otg_dbg_mode;
  1223. static int msm_otg_debugfs_init(struct msm_otg *motg)
  1224. {
  1225. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  1226. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  1227. return -ENODEV;
  1228. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  1229. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  1230. if (!msm_otg_dbg_mode) {
  1231. debugfs_remove(msm_otg_dbg_root);
  1232. msm_otg_dbg_root = NULL;
  1233. return -ENODEV;
  1234. }
  1235. return 0;
  1236. }
  1237. static void msm_otg_debugfs_cleanup(void)
  1238. {
  1239. debugfs_remove(msm_otg_dbg_mode);
  1240. debugfs_remove(msm_otg_dbg_root);
  1241. }
  1242. static int __init msm_otg_probe(struct platform_device *pdev)
  1243. {
  1244. int ret = 0;
  1245. struct resource *res;
  1246. struct msm_otg *motg;
  1247. struct usb_phy *phy;
  1248. dev_info(&pdev->dev, "msm_otg probe\n");
  1249. if (!pdev->dev.platform_data) {
  1250. dev_err(&pdev->dev, "No platform data given. Bailing out\n");
  1251. return -ENODEV;
  1252. }
  1253. motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
  1254. if (!motg) {
  1255. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  1256. return -ENOMEM;
  1257. }
  1258. motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
  1259. if (!motg->phy.otg) {
  1260. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  1261. return -ENOMEM;
  1262. }
  1263. motg->pdata = pdev->dev.platform_data;
  1264. phy = &motg->phy;
  1265. phy->dev = &pdev->dev;
  1266. motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
  1267. if (IS_ERR(motg->phy_reset_clk)) {
  1268. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  1269. ret = PTR_ERR(motg->phy_reset_clk);
  1270. goto free_motg;
  1271. }
  1272. motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
  1273. if (IS_ERR(motg->clk)) {
  1274. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  1275. ret = PTR_ERR(motg->clk);
  1276. goto put_phy_reset_clk;
  1277. }
  1278. clk_set_rate(motg->clk, 60000000);
  1279. /*
  1280. * If USB Core is running its protocol engine based on CORE CLK,
  1281. * CORE CLK must be running at >55Mhz for correct HSUSB
  1282. * operation and USB core cannot tolerate frequency changes on
  1283. * CORE CLK. For such USB cores, vote for maximum clk frequency
  1284. * on pclk source
  1285. */
  1286. if (motg->pdata->pclk_src_name) {
  1287. motg->pclk_src = clk_get(&pdev->dev,
  1288. motg->pdata->pclk_src_name);
  1289. if (IS_ERR(motg->pclk_src))
  1290. goto put_clk;
  1291. clk_set_rate(motg->pclk_src, INT_MAX);
  1292. clk_enable(motg->pclk_src);
  1293. } else
  1294. motg->pclk_src = ERR_PTR(-ENOENT);
  1295. motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
  1296. if (IS_ERR(motg->pclk)) {
  1297. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  1298. ret = PTR_ERR(motg->pclk);
  1299. goto put_pclk_src;
  1300. }
  1301. /*
  1302. * USB core clock is not present on all MSM chips. This
  1303. * clock is introduced to remove the dependency on AXI
  1304. * bus frequency.
  1305. */
  1306. motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
  1307. if (IS_ERR(motg->core_clk))
  1308. motg->core_clk = NULL;
  1309. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1310. if (!res) {
  1311. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  1312. ret = -ENODEV;
  1313. goto put_core_clk;
  1314. }
  1315. motg->regs = ioremap(res->start, resource_size(res));
  1316. if (!motg->regs) {
  1317. dev_err(&pdev->dev, "ioremap failed\n");
  1318. ret = -ENOMEM;
  1319. goto put_core_clk;
  1320. }
  1321. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  1322. motg->irq = platform_get_irq(pdev, 0);
  1323. if (!motg->irq) {
  1324. dev_err(&pdev->dev, "platform_get_irq failed\n");
  1325. ret = -ENODEV;
  1326. goto free_regs;
  1327. }
  1328. clk_enable(motg->clk);
  1329. clk_enable(motg->pclk);
  1330. ret = msm_hsusb_init_vddcx(motg, 1);
  1331. if (ret) {
  1332. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  1333. goto free_regs;
  1334. }
  1335. ret = msm_hsusb_ldo_init(motg, 1);
  1336. if (ret) {
  1337. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  1338. goto vddcx_exit;
  1339. }
  1340. ret = msm_hsusb_ldo_set_mode(1);
  1341. if (ret) {
  1342. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  1343. goto ldo_exit;
  1344. }
  1345. if (motg->core_clk)
  1346. clk_enable(motg->core_clk);
  1347. writel(0, USB_USBINTR);
  1348. writel(0, USB_OTGSC);
  1349. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  1350. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  1351. ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
  1352. "msm_otg", motg);
  1353. if (ret) {
  1354. dev_err(&pdev->dev, "request irq failed\n");
  1355. goto disable_clks;
  1356. }
  1357. phy->init = msm_otg_reset;
  1358. phy->set_power = msm_otg_set_power;
  1359. phy->io_ops = &msm_otg_io_ops;
  1360. phy->otg->phy = &motg->phy;
  1361. phy->otg->set_host = msm_otg_set_host;
  1362. phy->otg->set_peripheral = msm_otg_set_peripheral;
  1363. ret = usb_set_transceiver(&motg->phy);
  1364. if (ret) {
  1365. dev_err(&pdev->dev, "usb_set_transceiver failed\n");
  1366. goto free_irq;
  1367. }
  1368. platform_set_drvdata(pdev, motg);
  1369. device_init_wakeup(&pdev->dev, 1);
  1370. if (motg->pdata->mode == USB_OTG &&
  1371. motg->pdata->otg_control == OTG_USER_CONTROL) {
  1372. ret = msm_otg_debugfs_init(motg);
  1373. if (ret)
  1374. dev_dbg(&pdev->dev, "mode debugfs file is"
  1375. "not available\n");
  1376. }
  1377. pm_runtime_set_active(&pdev->dev);
  1378. pm_runtime_enable(&pdev->dev);
  1379. return 0;
  1380. free_irq:
  1381. free_irq(motg->irq, motg);
  1382. disable_clks:
  1383. clk_disable(motg->pclk);
  1384. clk_disable(motg->clk);
  1385. ldo_exit:
  1386. msm_hsusb_ldo_init(motg, 0);
  1387. vddcx_exit:
  1388. msm_hsusb_init_vddcx(motg, 0);
  1389. free_regs:
  1390. iounmap(motg->regs);
  1391. put_core_clk:
  1392. if (motg->core_clk)
  1393. clk_put(motg->core_clk);
  1394. clk_put(motg->pclk);
  1395. put_pclk_src:
  1396. if (!IS_ERR(motg->pclk_src)) {
  1397. clk_disable(motg->pclk_src);
  1398. clk_put(motg->pclk_src);
  1399. }
  1400. put_clk:
  1401. clk_put(motg->clk);
  1402. put_phy_reset_clk:
  1403. clk_put(motg->phy_reset_clk);
  1404. free_motg:
  1405. kfree(motg->phy.otg);
  1406. kfree(motg);
  1407. return ret;
  1408. }
  1409. static int __devexit msm_otg_remove(struct platform_device *pdev)
  1410. {
  1411. struct msm_otg *motg = platform_get_drvdata(pdev);
  1412. struct usb_phy *phy = &motg->phy;
  1413. int cnt = 0;
  1414. if (phy->otg->host || phy->otg->gadget)
  1415. return -EBUSY;
  1416. msm_otg_debugfs_cleanup();
  1417. cancel_delayed_work_sync(&motg->chg_work);
  1418. cancel_work_sync(&motg->sm_work);
  1419. pm_runtime_resume(&pdev->dev);
  1420. device_init_wakeup(&pdev->dev, 0);
  1421. pm_runtime_disable(&pdev->dev);
  1422. usb_set_transceiver(NULL);
  1423. free_irq(motg->irq, motg);
  1424. /*
  1425. * Put PHY in low power mode.
  1426. */
  1427. ulpi_read(phy, 0x14);
  1428. ulpi_write(phy, 0x08, 0x09);
  1429. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  1430. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  1431. if (readl(USB_PORTSC) & PORTSC_PHCD)
  1432. break;
  1433. udelay(1);
  1434. cnt++;
  1435. }
  1436. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  1437. dev_err(phy->dev, "Unable to suspend PHY\n");
  1438. clk_disable(motg->pclk);
  1439. clk_disable(motg->clk);
  1440. if (motg->core_clk)
  1441. clk_disable(motg->core_clk);
  1442. if (!IS_ERR(motg->pclk_src)) {
  1443. clk_disable(motg->pclk_src);
  1444. clk_put(motg->pclk_src);
  1445. }
  1446. msm_hsusb_ldo_init(motg, 0);
  1447. iounmap(motg->regs);
  1448. pm_runtime_set_suspended(&pdev->dev);
  1449. clk_put(motg->phy_reset_clk);
  1450. clk_put(motg->pclk);
  1451. clk_put(motg->clk);
  1452. if (motg->core_clk)
  1453. clk_put(motg->core_clk);
  1454. kfree(motg->phy.otg);
  1455. kfree(motg);
  1456. return 0;
  1457. }
  1458. #ifdef CONFIG_PM_RUNTIME
  1459. static int msm_otg_runtime_idle(struct device *dev)
  1460. {
  1461. struct msm_otg *motg = dev_get_drvdata(dev);
  1462. struct usb_otg *otg = motg->phy.otg;
  1463. dev_dbg(dev, "OTG runtime idle\n");
  1464. /*
  1465. * It is observed some times that a spurious interrupt
  1466. * comes when PHY is put into LPM immediately after PHY reset.
  1467. * This 1 sec delay also prevents entering into LPM immediately
  1468. * after asynchronous interrupt.
  1469. */
  1470. if (otg->phy->state != OTG_STATE_UNDEFINED)
  1471. pm_schedule_suspend(dev, 1000);
  1472. return -EAGAIN;
  1473. }
  1474. static int msm_otg_runtime_suspend(struct device *dev)
  1475. {
  1476. struct msm_otg *motg = dev_get_drvdata(dev);
  1477. dev_dbg(dev, "OTG runtime suspend\n");
  1478. return msm_otg_suspend(motg);
  1479. }
  1480. static int msm_otg_runtime_resume(struct device *dev)
  1481. {
  1482. struct msm_otg *motg = dev_get_drvdata(dev);
  1483. dev_dbg(dev, "OTG runtime resume\n");
  1484. return msm_otg_resume(motg);
  1485. }
  1486. #endif
  1487. #ifdef CONFIG_PM_SLEEP
  1488. static int msm_otg_pm_suspend(struct device *dev)
  1489. {
  1490. struct msm_otg *motg = dev_get_drvdata(dev);
  1491. dev_dbg(dev, "OTG PM suspend\n");
  1492. return msm_otg_suspend(motg);
  1493. }
  1494. static int msm_otg_pm_resume(struct device *dev)
  1495. {
  1496. struct msm_otg *motg = dev_get_drvdata(dev);
  1497. int ret;
  1498. dev_dbg(dev, "OTG PM resume\n");
  1499. ret = msm_otg_resume(motg);
  1500. if (ret)
  1501. return ret;
  1502. /*
  1503. * Runtime PM Documentation recommends bringing the
  1504. * device to full powered state upon resume.
  1505. */
  1506. pm_runtime_disable(dev);
  1507. pm_runtime_set_active(dev);
  1508. pm_runtime_enable(dev);
  1509. return 0;
  1510. }
  1511. #endif
  1512. #ifdef CONFIG_PM
  1513. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  1514. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  1515. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  1516. msm_otg_runtime_idle)
  1517. };
  1518. #endif
  1519. static struct platform_driver msm_otg_driver = {
  1520. .remove = __devexit_p(msm_otg_remove),
  1521. .driver = {
  1522. .name = DRIVER_NAME,
  1523. .owner = THIS_MODULE,
  1524. #ifdef CONFIG_PM
  1525. .pm = &msm_otg_dev_pm_ops,
  1526. #endif
  1527. },
  1528. };
  1529. static int __init msm_otg_init(void)
  1530. {
  1531. return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
  1532. }
  1533. static void __exit msm_otg_exit(void)
  1534. {
  1535. platform_driver_unregister(&msm_otg_driver);
  1536. }
  1537. module_init(msm_otg_init);
  1538. module_exit(msm_otg_exit);
  1539. MODULE_LICENSE("GPL v2");
  1540. MODULE_DESCRIPTION("MSM USB transceiver driver");