phy-msm-usb.c 44 KB

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