msm_otg.c 40 KB

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