phy-msm-usb.c 48 KB

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