phy-msm-usb.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  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. /*
  627. * Kick the state machine work, if peripheral is not supported
  628. * or peripheral is already registered with us.
  629. */
  630. if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
  631. pm_runtime_get_sync(otg->usb_phy->dev);
  632. schedule_work(&motg->sm_work);
  633. }
  634. return 0;
  635. }
  636. static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
  637. {
  638. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  639. struct msm_otg_platform_data *pdata = motg->pdata;
  640. if (!phy->otg->gadget)
  641. return;
  642. if (on) {
  643. dev_dbg(phy->dev, "gadget on\n");
  644. /*
  645. * Some boards have a switch cotrolled by gpio
  646. * to enable/disable internal HUB. Disable internal
  647. * HUB before kicking the gadget.
  648. */
  649. if (pdata->setup_gpio)
  650. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  651. usb_gadget_vbus_connect(phy->otg->gadget);
  652. } else {
  653. dev_dbg(phy->dev, "gadget off\n");
  654. usb_gadget_vbus_disconnect(phy->otg->gadget);
  655. if (pdata->setup_gpio)
  656. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  657. }
  658. }
  659. static int msm_otg_set_peripheral(struct usb_otg *otg,
  660. struct usb_gadget *gadget)
  661. {
  662. struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
  663. /*
  664. * Fail peripheral registration if this board can support
  665. * only host configuration.
  666. */
  667. if (motg->pdata->mode == USB_DR_MODE_HOST) {
  668. dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
  669. return -ENODEV;
  670. }
  671. if (!gadget) {
  672. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  673. pm_runtime_get_sync(otg->usb_phy->dev);
  674. msm_otg_start_peripheral(otg->usb_phy, 0);
  675. otg->gadget = NULL;
  676. otg->state = OTG_STATE_UNDEFINED;
  677. schedule_work(&motg->sm_work);
  678. } else {
  679. otg->gadget = NULL;
  680. }
  681. return 0;
  682. }
  683. otg->gadget = gadget;
  684. dev_dbg(otg->usb_phy->dev,
  685. "peripheral driver registered w/ tranceiver\n");
  686. /*
  687. * Kick the state machine work, if host is not supported
  688. * or host is already registered with us.
  689. */
  690. if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
  691. pm_runtime_get_sync(otg->usb_phy->dev);
  692. schedule_work(&motg->sm_work);
  693. }
  694. return 0;
  695. }
  696. static bool msm_chg_check_secondary_det(struct msm_otg *motg)
  697. {
  698. struct usb_phy *phy = &motg->phy;
  699. u32 chg_det;
  700. bool ret = false;
  701. switch (motg->pdata->phy_type) {
  702. case CI_45NM_INTEGRATED_PHY:
  703. chg_det = ulpi_read(phy, 0x34);
  704. ret = chg_det & (1 << 4);
  705. break;
  706. case SNPS_28NM_INTEGRATED_PHY:
  707. chg_det = ulpi_read(phy, 0x87);
  708. ret = chg_det & 1;
  709. break;
  710. default:
  711. break;
  712. }
  713. return ret;
  714. }
  715. static void msm_chg_enable_secondary_det(struct msm_otg *motg)
  716. {
  717. struct usb_phy *phy = &motg->phy;
  718. u32 chg_det;
  719. switch (motg->pdata->phy_type) {
  720. case CI_45NM_INTEGRATED_PHY:
  721. chg_det = ulpi_read(phy, 0x34);
  722. /* Turn off charger block */
  723. chg_det |= ~(1 << 1);
  724. ulpi_write(phy, chg_det, 0x34);
  725. udelay(20);
  726. /* control chg block via ULPI */
  727. chg_det &= ~(1 << 3);
  728. ulpi_write(phy, chg_det, 0x34);
  729. /* put it in host mode for enabling D- source */
  730. chg_det &= ~(1 << 2);
  731. ulpi_write(phy, chg_det, 0x34);
  732. /* Turn on chg detect block */
  733. chg_det &= ~(1 << 1);
  734. ulpi_write(phy, chg_det, 0x34);
  735. udelay(20);
  736. /* enable chg detection */
  737. chg_det &= ~(1 << 0);
  738. ulpi_write(phy, chg_det, 0x34);
  739. break;
  740. case SNPS_28NM_INTEGRATED_PHY:
  741. /*
  742. * Configure DM as current source, DP as current sink
  743. * and enable battery charging comparators.
  744. */
  745. ulpi_write(phy, 0x8, 0x85);
  746. ulpi_write(phy, 0x2, 0x85);
  747. ulpi_write(phy, 0x1, 0x85);
  748. break;
  749. default:
  750. break;
  751. }
  752. }
  753. static bool msm_chg_check_primary_det(struct msm_otg *motg)
  754. {
  755. struct usb_phy *phy = &motg->phy;
  756. u32 chg_det;
  757. bool ret = false;
  758. switch (motg->pdata->phy_type) {
  759. case CI_45NM_INTEGRATED_PHY:
  760. chg_det = ulpi_read(phy, 0x34);
  761. ret = chg_det & (1 << 4);
  762. break;
  763. case SNPS_28NM_INTEGRATED_PHY:
  764. chg_det = ulpi_read(phy, 0x87);
  765. ret = chg_det & 1;
  766. break;
  767. default:
  768. break;
  769. }
  770. return ret;
  771. }
  772. static void msm_chg_enable_primary_det(struct msm_otg *motg)
  773. {
  774. struct usb_phy *phy = &motg->phy;
  775. u32 chg_det;
  776. switch (motg->pdata->phy_type) {
  777. case CI_45NM_INTEGRATED_PHY:
  778. chg_det = ulpi_read(phy, 0x34);
  779. /* enable chg detection */
  780. chg_det &= ~(1 << 0);
  781. ulpi_write(phy, chg_det, 0x34);
  782. break;
  783. case SNPS_28NM_INTEGRATED_PHY:
  784. /*
  785. * Configure DP as current source, DM as current sink
  786. * and enable battery charging comparators.
  787. */
  788. ulpi_write(phy, 0x2, 0x85);
  789. ulpi_write(phy, 0x1, 0x85);
  790. break;
  791. default:
  792. break;
  793. }
  794. }
  795. static bool msm_chg_check_dcd(struct msm_otg *motg)
  796. {
  797. struct usb_phy *phy = &motg->phy;
  798. u32 line_state;
  799. bool ret = false;
  800. switch (motg->pdata->phy_type) {
  801. case CI_45NM_INTEGRATED_PHY:
  802. line_state = ulpi_read(phy, 0x15);
  803. ret = !(line_state & 1);
  804. break;
  805. case SNPS_28NM_INTEGRATED_PHY:
  806. line_state = ulpi_read(phy, 0x87);
  807. ret = line_state & 2;
  808. break;
  809. default:
  810. break;
  811. }
  812. return ret;
  813. }
  814. static void msm_chg_disable_dcd(struct msm_otg *motg)
  815. {
  816. struct usb_phy *phy = &motg->phy;
  817. u32 chg_det;
  818. switch (motg->pdata->phy_type) {
  819. case CI_45NM_INTEGRATED_PHY:
  820. chg_det = ulpi_read(phy, 0x34);
  821. chg_det &= ~(1 << 5);
  822. ulpi_write(phy, chg_det, 0x34);
  823. break;
  824. case SNPS_28NM_INTEGRATED_PHY:
  825. ulpi_write(phy, 0x10, 0x86);
  826. break;
  827. default:
  828. break;
  829. }
  830. }
  831. static void msm_chg_enable_dcd(struct msm_otg *motg)
  832. {
  833. struct usb_phy *phy = &motg->phy;
  834. u32 chg_det;
  835. switch (motg->pdata->phy_type) {
  836. case CI_45NM_INTEGRATED_PHY:
  837. chg_det = ulpi_read(phy, 0x34);
  838. /* Turn on D+ current source */
  839. chg_det |= (1 << 5);
  840. ulpi_write(phy, chg_det, 0x34);
  841. break;
  842. case SNPS_28NM_INTEGRATED_PHY:
  843. /* Data contact detection enable */
  844. ulpi_write(phy, 0x10, 0x85);
  845. break;
  846. default:
  847. break;
  848. }
  849. }
  850. static void msm_chg_block_on(struct msm_otg *motg)
  851. {
  852. struct usb_phy *phy = &motg->phy;
  853. u32 func_ctrl, chg_det;
  854. /* put the controller in non-driving mode */
  855. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  856. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  857. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  858. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  859. switch (motg->pdata->phy_type) {
  860. case CI_45NM_INTEGRATED_PHY:
  861. chg_det = ulpi_read(phy, 0x34);
  862. /* control chg block via ULPI */
  863. chg_det &= ~(1 << 3);
  864. ulpi_write(phy, chg_det, 0x34);
  865. /* Turn on chg detect block */
  866. chg_det &= ~(1 << 1);
  867. ulpi_write(phy, chg_det, 0x34);
  868. udelay(20);
  869. break;
  870. case SNPS_28NM_INTEGRATED_PHY:
  871. /* Clear charger detecting control bits */
  872. ulpi_write(phy, 0x3F, 0x86);
  873. /* Clear alt interrupt latch and enable bits */
  874. ulpi_write(phy, 0x1F, 0x92);
  875. ulpi_write(phy, 0x1F, 0x95);
  876. udelay(100);
  877. break;
  878. default:
  879. break;
  880. }
  881. }
  882. static void msm_chg_block_off(struct msm_otg *motg)
  883. {
  884. struct usb_phy *phy = &motg->phy;
  885. u32 func_ctrl, chg_det;
  886. switch (motg->pdata->phy_type) {
  887. case CI_45NM_INTEGRATED_PHY:
  888. chg_det = ulpi_read(phy, 0x34);
  889. /* Turn off charger block */
  890. chg_det |= ~(1 << 1);
  891. ulpi_write(phy, chg_det, 0x34);
  892. break;
  893. case SNPS_28NM_INTEGRATED_PHY:
  894. /* Clear charger detecting control bits */
  895. ulpi_write(phy, 0x3F, 0x86);
  896. /* Clear alt interrupt latch and enable bits */
  897. ulpi_write(phy, 0x1F, 0x92);
  898. ulpi_write(phy, 0x1F, 0x95);
  899. break;
  900. default:
  901. break;
  902. }
  903. /* put the controller in normal mode */
  904. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  905. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  906. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  907. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  908. }
  909. #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
  910. #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
  911. #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
  912. #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
  913. static void msm_chg_detect_work(struct work_struct *w)
  914. {
  915. struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
  916. struct usb_phy *phy = &motg->phy;
  917. bool is_dcd, tmout, vout;
  918. unsigned long delay;
  919. dev_dbg(phy->dev, "chg detection work\n");
  920. switch (motg->chg_state) {
  921. case USB_CHG_STATE_UNDEFINED:
  922. pm_runtime_get_sync(phy->dev);
  923. msm_chg_block_on(motg);
  924. msm_chg_enable_dcd(motg);
  925. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  926. motg->dcd_retries = 0;
  927. delay = MSM_CHG_DCD_POLL_TIME;
  928. break;
  929. case USB_CHG_STATE_WAIT_FOR_DCD:
  930. is_dcd = msm_chg_check_dcd(motg);
  931. tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
  932. if (is_dcd || tmout) {
  933. msm_chg_disable_dcd(motg);
  934. msm_chg_enable_primary_det(motg);
  935. delay = MSM_CHG_PRIMARY_DET_TIME;
  936. motg->chg_state = USB_CHG_STATE_DCD_DONE;
  937. } else {
  938. delay = MSM_CHG_DCD_POLL_TIME;
  939. }
  940. break;
  941. case USB_CHG_STATE_DCD_DONE:
  942. vout = msm_chg_check_primary_det(motg);
  943. if (vout) {
  944. msm_chg_enable_secondary_det(motg);
  945. delay = MSM_CHG_SECONDARY_DET_TIME;
  946. motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  947. } else {
  948. motg->chg_type = USB_SDP_CHARGER;
  949. motg->chg_state = USB_CHG_STATE_DETECTED;
  950. delay = 0;
  951. }
  952. break;
  953. case USB_CHG_STATE_PRIMARY_DONE:
  954. vout = msm_chg_check_secondary_det(motg);
  955. if (vout)
  956. motg->chg_type = USB_DCP_CHARGER;
  957. else
  958. motg->chg_type = USB_CDP_CHARGER;
  959. motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
  960. /* fall through */
  961. case USB_CHG_STATE_SECONDARY_DONE:
  962. motg->chg_state = USB_CHG_STATE_DETECTED;
  963. case USB_CHG_STATE_DETECTED:
  964. msm_chg_block_off(motg);
  965. dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
  966. schedule_work(&motg->sm_work);
  967. return;
  968. default:
  969. return;
  970. }
  971. schedule_delayed_work(&motg->chg_work, delay);
  972. }
  973. /*
  974. * We support OTG, Peripheral only and Host only configurations. In case
  975. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  976. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  977. * enabled when switch is controlled by user and default mode is supplied
  978. * by board file, which can be changed by userspace later.
  979. */
  980. static void msm_otg_init_sm(struct msm_otg *motg)
  981. {
  982. struct msm_otg_platform_data *pdata = motg->pdata;
  983. u32 otgsc = readl(USB_OTGSC);
  984. switch (pdata->mode) {
  985. case USB_DR_MODE_OTG:
  986. if (pdata->otg_control == OTG_PHY_CONTROL) {
  987. if (otgsc & OTGSC_ID)
  988. set_bit(ID, &motg->inputs);
  989. else
  990. clear_bit(ID, &motg->inputs);
  991. if (otgsc & OTGSC_BSV)
  992. set_bit(B_SESS_VLD, &motg->inputs);
  993. else
  994. clear_bit(B_SESS_VLD, &motg->inputs);
  995. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  996. set_bit(ID, &motg->inputs);
  997. clear_bit(B_SESS_VLD, &motg->inputs);
  998. }
  999. break;
  1000. case USB_DR_MODE_HOST:
  1001. clear_bit(ID, &motg->inputs);
  1002. break;
  1003. case USB_DR_MODE_PERIPHERAL:
  1004. set_bit(ID, &motg->inputs);
  1005. if (otgsc & OTGSC_BSV)
  1006. set_bit(B_SESS_VLD, &motg->inputs);
  1007. else
  1008. clear_bit(B_SESS_VLD, &motg->inputs);
  1009. break;
  1010. default:
  1011. break;
  1012. }
  1013. }
  1014. static void msm_otg_sm_work(struct work_struct *w)
  1015. {
  1016. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  1017. struct usb_otg *otg = motg->phy.otg;
  1018. switch (otg->state) {
  1019. case OTG_STATE_UNDEFINED:
  1020. dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
  1021. msm_otg_reset(otg->usb_phy);
  1022. msm_otg_init_sm(motg);
  1023. otg->state = OTG_STATE_B_IDLE;
  1024. /* FALL THROUGH */
  1025. case OTG_STATE_B_IDLE:
  1026. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
  1027. if (!test_bit(ID, &motg->inputs) && otg->host) {
  1028. /* disable BSV bit */
  1029. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  1030. msm_otg_start_host(otg->usb_phy, 1);
  1031. otg->state = OTG_STATE_A_HOST;
  1032. } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1033. switch (motg->chg_state) {
  1034. case USB_CHG_STATE_UNDEFINED:
  1035. msm_chg_detect_work(&motg->chg_work.work);
  1036. break;
  1037. case USB_CHG_STATE_DETECTED:
  1038. switch (motg->chg_type) {
  1039. case USB_DCP_CHARGER:
  1040. msm_otg_notify_charger(motg,
  1041. IDEV_CHG_MAX);
  1042. break;
  1043. case USB_CDP_CHARGER:
  1044. msm_otg_notify_charger(motg,
  1045. IDEV_CHG_MAX);
  1046. msm_otg_start_peripheral(otg->usb_phy,
  1047. 1);
  1048. otg->state
  1049. = OTG_STATE_B_PERIPHERAL;
  1050. break;
  1051. case USB_SDP_CHARGER:
  1052. msm_otg_notify_charger(motg, IUNIT);
  1053. msm_otg_start_peripheral(otg->usb_phy,
  1054. 1);
  1055. otg->state
  1056. = OTG_STATE_B_PERIPHERAL;
  1057. break;
  1058. default:
  1059. break;
  1060. }
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. } else {
  1066. /*
  1067. * If charger detection work is pending, decrement
  1068. * the pm usage counter to balance with the one that
  1069. * is incremented in charger detection work.
  1070. */
  1071. if (cancel_delayed_work_sync(&motg->chg_work)) {
  1072. pm_runtime_put_sync(otg->usb_phy->dev);
  1073. msm_otg_reset(otg->usb_phy);
  1074. }
  1075. msm_otg_notify_charger(motg, 0);
  1076. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1077. motg->chg_type = USB_INVALID_CHARGER;
  1078. }
  1079. if (otg->state == OTG_STATE_B_IDLE)
  1080. pm_runtime_put_sync(otg->usb_phy->dev);
  1081. break;
  1082. case OTG_STATE_B_PERIPHERAL:
  1083. dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
  1084. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  1085. !test_bit(ID, &motg->inputs)) {
  1086. msm_otg_notify_charger(motg, 0);
  1087. msm_otg_start_peripheral(otg->usb_phy, 0);
  1088. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1089. motg->chg_type = USB_INVALID_CHARGER;
  1090. otg->state = OTG_STATE_B_IDLE;
  1091. msm_otg_reset(otg->usb_phy);
  1092. schedule_work(w);
  1093. }
  1094. break;
  1095. case OTG_STATE_A_HOST:
  1096. dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
  1097. if (test_bit(ID, &motg->inputs)) {
  1098. msm_otg_start_host(otg->usb_phy, 0);
  1099. otg->state = OTG_STATE_B_IDLE;
  1100. msm_otg_reset(otg->usb_phy);
  1101. schedule_work(w);
  1102. }
  1103. break;
  1104. default:
  1105. break;
  1106. }
  1107. }
  1108. static irqreturn_t msm_otg_irq(int irq, void *data)
  1109. {
  1110. struct msm_otg *motg = data;
  1111. struct usb_phy *phy = &motg->phy;
  1112. u32 otgsc = 0;
  1113. if (atomic_read(&motg->in_lpm)) {
  1114. disable_irq_nosync(irq);
  1115. motg->async_int = 1;
  1116. pm_runtime_get(phy->dev);
  1117. return IRQ_HANDLED;
  1118. }
  1119. otgsc = readl(USB_OTGSC);
  1120. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  1121. return IRQ_NONE;
  1122. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  1123. if (otgsc & OTGSC_ID)
  1124. set_bit(ID, &motg->inputs);
  1125. else
  1126. clear_bit(ID, &motg->inputs);
  1127. dev_dbg(phy->dev, "ID set/clear\n");
  1128. pm_runtime_get_noresume(phy->dev);
  1129. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  1130. if (otgsc & OTGSC_BSV)
  1131. set_bit(B_SESS_VLD, &motg->inputs);
  1132. else
  1133. clear_bit(B_SESS_VLD, &motg->inputs);
  1134. dev_dbg(phy->dev, "BSV set/clear\n");
  1135. pm_runtime_get_noresume(phy->dev);
  1136. }
  1137. writel(otgsc, USB_OTGSC);
  1138. schedule_work(&motg->sm_work);
  1139. return IRQ_HANDLED;
  1140. }
  1141. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  1142. {
  1143. struct msm_otg *motg = s->private;
  1144. struct usb_otg *otg = motg->phy.otg;
  1145. switch (otg->state) {
  1146. case OTG_STATE_A_HOST:
  1147. seq_puts(s, "host\n");
  1148. break;
  1149. case OTG_STATE_B_PERIPHERAL:
  1150. seq_puts(s, "peripheral\n");
  1151. break;
  1152. default:
  1153. seq_puts(s, "none\n");
  1154. break;
  1155. }
  1156. return 0;
  1157. }
  1158. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  1159. {
  1160. return single_open(file, msm_otg_mode_show, inode->i_private);
  1161. }
  1162. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  1163. size_t count, loff_t *ppos)
  1164. {
  1165. struct seq_file *s = file->private_data;
  1166. struct msm_otg *motg = s->private;
  1167. char buf[16];
  1168. struct usb_otg *otg = motg->phy.otg;
  1169. int status = count;
  1170. enum usb_dr_mode req_mode;
  1171. memset(buf, 0x00, sizeof(buf));
  1172. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  1173. status = -EFAULT;
  1174. goto out;
  1175. }
  1176. if (!strncmp(buf, "host", 4)) {
  1177. req_mode = USB_DR_MODE_HOST;
  1178. } else if (!strncmp(buf, "peripheral", 10)) {
  1179. req_mode = USB_DR_MODE_PERIPHERAL;
  1180. } else if (!strncmp(buf, "none", 4)) {
  1181. req_mode = USB_DR_MODE_UNKNOWN;
  1182. } else {
  1183. status = -EINVAL;
  1184. goto out;
  1185. }
  1186. switch (req_mode) {
  1187. case USB_DR_MODE_UNKNOWN:
  1188. switch (otg->state) {
  1189. case OTG_STATE_A_HOST:
  1190. case OTG_STATE_B_PERIPHERAL:
  1191. set_bit(ID, &motg->inputs);
  1192. clear_bit(B_SESS_VLD, &motg->inputs);
  1193. break;
  1194. default:
  1195. goto out;
  1196. }
  1197. break;
  1198. case USB_DR_MODE_PERIPHERAL:
  1199. switch (otg->state) {
  1200. case OTG_STATE_B_IDLE:
  1201. case OTG_STATE_A_HOST:
  1202. set_bit(ID, &motg->inputs);
  1203. set_bit(B_SESS_VLD, &motg->inputs);
  1204. break;
  1205. default:
  1206. goto out;
  1207. }
  1208. break;
  1209. case USB_DR_MODE_HOST:
  1210. switch (otg->state) {
  1211. case OTG_STATE_B_IDLE:
  1212. case OTG_STATE_B_PERIPHERAL:
  1213. clear_bit(ID, &motg->inputs);
  1214. break;
  1215. default:
  1216. goto out;
  1217. }
  1218. break;
  1219. default:
  1220. goto out;
  1221. }
  1222. pm_runtime_get_sync(otg->usb_phy->dev);
  1223. schedule_work(&motg->sm_work);
  1224. out:
  1225. return status;
  1226. }
  1227. static const struct file_operations msm_otg_mode_fops = {
  1228. .open = msm_otg_mode_open,
  1229. .read = seq_read,
  1230. .write = msm_otg_mode_write,
  1231. .llseek = seq_lseek,
  1232. .release = single_release,
  1233. };
  1234. static struct dentry *msm_otg_dbg_root;
  1235. static struct dentry *msm_otg_dbg_mode;
  1236. static int msm_otg_debugfs_init(struct msm_otg *motg)
  1237. {
  1238. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  1239. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  1240. return -ENODEV;
  1241. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  1242. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  1243. if (!msm_otg_dbg_mode) {
  1244. debugfs_remove(msm_otg_dbg_root);
  1245. msm_otg_dbg_root = NULL;
  1246. return -ENODEV;
  1247. }
  1248. return 0;
  1249. }
  1250. static void msm_otg_debugfs_cleanup(void)
  1251. {
  1252. debugfs_remove(msm_otg_dbg_mode);
  1253. debugfs_remove(msm_otg_dbg_root);
  1254. }
  1255. static const struct of_device_id msm_otg_dt_match[] = {
  1256. {
  1257. .compatible = "qcom,usb-otg-ci",
  1258. .data = (void *) CI_45NM_INTEGRATED_PHY
  1259. },
  1260. {
  1261. .compatible = "qcom,usb-otg-snps",
  1262. .data = (void *) SNPS_28NM_INTEGRATED_PHY
  1263. },
  1264. { }
  1265. };
  1266. MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
  1267. static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event,
  1268. void *ptr)
  1269. {
  1270. struct msm_usb_cable *vbus = container_of(nb, struct msm_usb_cable, nb);
  1271. struct msm_otg *motg = container_of(vbus, struct msm_otg, vbus);
  1272. if (event)
  1273. set_bit(B_SESS_VLD, &motg->inputs);
  1274. else
  1275. clear_bit(B_SESS_VLD, &motg->inputs);
  1276. if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1277. /* Switch D+/D- lines to Device connector */
  1278. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1279. } else {
  1280. /* Switch D+/D- lines to Hub */
  1281. gpiod_set_value_cansleep(motg->switch_gpio, 1);
  1282. }
  1283. schedule_work(&motg->sm_work);
  1284. return NOTIFY_DONE;
  1285. }
  1286. static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event,
  1287. void *ptr)
  1288. {
  1289. struct msm_usb_cable *id = container_of(nb, struct msm_usb_cable, nb);
  1290. struct msm_otg *motg = container_of(id, struct msm_otg, id);
  1291. if (event)
  1292. clear_bit(ID, &motg->inputs);
  1293. else
  1294. set_bit(ID, &motg->inputs);
  1295. schedule_work(&motg->sm_work);
  1296. return NOTIFY_DONE;
  1297. }
  1298. static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
  1299. {
  1300. struct msm_otg_platform_data *pdata;
  1301. struct extcon_dev *ext_id, *ext_vbus;
  1302. struct device_node *node = pdev->dev.of_node;
  1303. struct property *prop;
  1304. int len, ret, words;
  1305. u32 val, tmp[3];
  1306. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1307. if (!pdata)
  1308. return -ENOMEM;
  1309. motg->pdata = pdata;
  1310. pdata->phy_type = (enum msm_usb_phy_type)of_device_get_match_data(&pdev->dev);
  1311. if (!pdata->phy_type)
  1312. return 1;
  1313. motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
  1314. if (IS_ERR(motg->link_rst))
  1315. return PTR_ERR(motg->link_rst);
  1316. motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
  1317. if (IS_ERR(motg->phy_rst))
  1318. motg->phy_rst = NULL;
  1319. pdata->mode = usb_get_dr_mode(&pdev->dev);
  1320. if (pdata->mode == USB_DR_MODE_UNKNOWN)
  1321. pdata->mode = USB_DR_MODE_OTG;
  1322. pdata->otg_control = OTG_PHY_CONTROL;
  1323. if (!of_property_read_u32(node, "qcom,otg-control", &val))
  1324. if (val == OTG_PMIC_CONTROL)
  1325. pdata->otg_control = val;
  1326. if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
  1327. motg->phy_number = val;
  1328. motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
  1329. motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
  1330. motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
  1331. if (of_get_property(node, "qcom,vdd-levels", &len) &&
  1332. len == sizeof(tmp)) {
  1333. of_property_read_u32_array(node, "qcom,vdd-levels",
  1334. tmp, len / sizeof(*tmp));
  1335. motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
  1336. motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
  1337. motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
  1338. }
  1339. motg->manual_pullup = of_property_read_bool(node, "qcom,manual-pullup");
  1340. motg->switch_gpio = devm_gpiod_get_optional(&pdev->dev, "switch",
  1341. GPIOD_OUT_LOW);
  1342. if (IS_ERR(motg->switch_gpio))
  1343. return PTR_ERR(motg->switch_gpio);
  1344. ext_id = ERR_PTR(-ENODEV);
  1345. ext_vbus = ERR_PTR(-ENODEV);
  1346. if (of_property_read_bool(node, "extcon")) {
  1347. /* Each one of them is not mandatory */
  1348. ext_vbus = extcon_get_edev_by_phandle(&pdev->dev, 0);
  1349. if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
  1350. return PTR_ERR(ext_vbus);
  1351. ext_id = extcon_get_edev_by_phandle(&pdev->dev, 1);
  1352. if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
  1353. return PTR_ERR(ext_id);
  1354. }
  1355. if (!IS_ERR(ext_vbus)) {
  1356. motg->vbus.extcon = ext_vbus;
  1357. motg->vbus.nb.notifier_call = msm_otg_vbus_notifier;
  1358. ret = extcon_register_notifier(ext_vbus, EXTCON_USB,
  1359. &motg->vbus.nb);
  1360. if (ret < 0) {
  1361. dev_err(&pdev->dev, "register VBUS notifier failed\n");
  1362. return ret;
  1363. }
  1364. ret = extcon_get_cable_state_(ext_vbus, EXTCON_USB);
  1365. if (ret)
  1366. set_bit(B_SESS_VLD, &motg->inputs);
  1367. else
  1368. clear_bit(B_SESS_VLD, &motg->inputs);
  1369. }
  1370. if (!IS_ERR(ext_id)) {
  1371. motg->id.extcon = ext_id;
  1372. motg->id.nb.notifier_call = msm_otg_id_notifier;
  1373. ret = extcon_register_notifier(ext_id, EXTCON_USB_HOST,
  1374. &motg->id.nb);
  1375. if (ret < 0) {
  1376. dev_err(&pdev->dev, "register ID notifier failed\n");
  1377. extcon_unregister_notifier(motg->vbus.extcon,
  1378. EXTCON_USB, &motg->vbus.nb);
  1379. return ret;
  1380. }
  1381. ret = extcon_get_cable_state_(ext_id, EXTCON_USB_HOST);
  1382. if (ret)
  1383. clear_bit(ID, &motg->inputs);
  1384. else
  1385. set_bit(ID, &motg->inputs);
  1386. }
  1387. prop = of_find_property(node, "qcom,phy-init-sequence", &len);
  1388. if (!prop || !len)
  1389. return 0;
  1390. words = len / sizeof(u32);
  1391. if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
  1392. dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
  1393. return 0;
  1394. }
  1395. pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  1396. if (!pdata->phy_init_seq)
  1397. return 0;
  1398. ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
  1399. pdata->phy_init_seq, words);
  1400. if (!ret)
  1401. pdata->phy_init_sz = words;
  1402. return 0;
  1403. }
  1404. static int msm_otg_reboot_notify(struct notifier_block *this,
  1405. unsigned long code, void *unused)
  1406. {
  1407. struct msm_otg *motg = container_of(this, struct msm_otg, reboot);
  1408. /*
  1409. * Ensure that D+/D- lines are routed to uB connector, so
  1410. * we could load bootloader/kernel at next reboot
  1411. */
  1412. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1413. return NOTIFY_DONE;
  1414. }
  1415. static int msm_otg_probe(struct platform_device *pdev)
  1416. {
  1417. struct regulator_bulk_data regs[3];
  1418. int ret = 0;
  1419. struct device_node *np = pdev->dev.of_node;
  1420. struct msm_otg_platform_data *pdata;
  1421. struct resource *res;
  1422. struct msm_otg *motg;
  1423. struct usb_phy *phy;
  1424. void __iomem *phy_select;
  1425. motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
  1426. if (!motg)
  1427. return -ENOMEM;
  1428. motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  1429. GFP_KERNEL);
  1430. if (!motg->phy.otg)
  1431. return -ENOMEM;
  1432. phy = &motg->phy;
  1433. phy->dev = &pdev->dev;
  1434. motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
  1435. if (IS_ERR(motg->clk)) {
  1436. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  1437. return PTR_ERR(motg->clk);
  1438. }
  1439. /*
  1440. * If USB Core is running its protocol engine based on CORE CLK,
  1441. * CORE CLK must be running at >55Mhz for correct HSUSB
  1442. * operation and USB core cannot tolerate frequency changes on
  1443. * CORE CLK.
  1444. */
  1445. motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
  1446. if (IS_ERR(motg->pclk)) {
  1447. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  1448. return PTR_ERR(motg->pclk);
  1449. }
  1450. /*
  1451. * USB core clock is not present on all MSM chips. This
  1452. * clock is introduced to remove the dependency on AXI
  1453. * bus frequency.
  1454. */
  1455. motg->core_clk = devm_clk_get(&pdev->dev,
  1456. np ? "alt_core" : "usb_hs_core_clk");
  1457. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1458. if (!res)
  1459. return -EINVAL;
  1460. motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  1461. if (!motg->regs)
  1462. return -ENOMEM;
  1463. pdata = dev_get_platdata(&pdev->dev);
  1464. if (!pdata) {
  1465. if (!np)
  1466. return -ENXIO;
  1467. ret = msm_otg_read_dt(pdev, motg);
  1468. if (ret)
  1469. return ret;
  1470. }
  1471. /*
  1472. * NOTE: The PHYs can be multiplexed between the chipidea controller
  1473. * and the dwc3 controller, using a single bit. It is important that
  1474. * the dwc3 driver does not set this bit in an incompatible way.
  1475. */
  1476. if (motg->phy_number) {
  1477. phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
  1478. if (!phy_select) {
  1479. ret = -ENOMEM;
  1480. goto unregister_extcon;
  1481. }
  1482. /* Enable second PHY with the OTG port */
  1483. writel(0x1, phy_select);
  1484. }
  1485. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  1486. motg->irq = platform_get_irq(pdev, 0);
  1487. if (motg->irq < 0) {
  1488. dev_err(&pdev->dev, "platform_get_irq failed\n");
  1489. ret = motg->irq;
  1490. goto unregister_extcon;
  1491. }
  1492. regs[0].supply = "vddcx";
  1493. regs[1].supply = "v3p3";
  1494. regs[2].supply = "v1p8";
  1495. ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
  1496. if (ret)
  1497. goto unregister_extcon;
  1498. motg->vddcx = regs[0].consumer;
  1499. motg->v3p3 = regs[1].consumer;
  1500. motg->v1p8 = regs[2].consumer;
  1501. clk_set_rate(motg->clk, 60000000);
  1502. clk_prepare_enable(motg->clk);
  1503. clk_prepare_enable(motg->pclk);
  1504. if (!IS_ERR(motg->core_clk))
  1505. clk_prepare_enable(motg->core_clk);
  1506. ret = msm_hsusb_init_vddcx(motg, 1);
  1507. if (ret) {
  1508. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  1509. goto disable_clks;
  1510. }
  1511. ret = msm_hsusb_ldo_init(motg, 1);
  1512. if (ret) {
  1513. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  1514. goto disable_vddcx;
  1515. }
  1516. ret = msm_hsusb_ldo_set_mode(motg, 1);
  1517. if (ret) {
  1518. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  1519. goto disable_ldo;
  1520. }
  1521. writel(0, USB_USBINTR);
  1522. writel(0, USB_OTGSC);
  1523. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  1524. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  1525. ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
  1526. "msm_otg", motg);
  1527. if (ret) {
  1528. dev_err(&pdev->dev, "request irq failed\n");
  1529. goto disable_ldo;
  1530. }
  1531. phy->init = msm_phy_init;
  1532. phy->set_power = msm_otg_set_power;
  1533. phy->notify_disconnect = msm_phy_notify_disconnect;
  1534. phy->type = USB_PHY_TYPE_USB2;
  1535. phy->io_ops = &msm_otg_io_ops;
  1536. phy->otg->usb_phy = &motg->phy;
  1537. phy->otg->set_host = msm_otg_set_host;
  1538. phy->otg->set_peripheral = msm_otg_set_peripheral;
  1539. msm_usb_reset(phy);
  1540. ret = usb_add_phy_dev(&motg->phy);
  1541. if (ret) {
  1542. dev_err(&pdev->dev, "usb_add_phy failed\n");
  1543. goto disable_ldo;
  1544. }
  1545. platform_set_drvdata(pdev, motg);
  1546. device_init_wakeup(&pdev->dev, 1);
  1547. if (motg->pdata->mode == USB_DR_MODE_OTG &&
  1548. motg->pdata->otg_control == OTG_USER_CONTROL) {
  1549. ret = msm_otg_debugfs_init(motg);
  1550. if (ret)
  1551. dev_dbg(&pdev->dev, "Can not create mode change file\n");
  1552. }
  1553. if (test_bit(B_SESS_VLD, &motg->inputs)) {
  1554. /* Switch D+/D- lines to Device connector */
  1555. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1556. } else {
  1557. /* Switch D+/D- lines to Hub */
  1558. gpiod_set_value_cansleep(motg->switch_gpio, 1);
  1559. }
  1560. motg->reboot.notifier_call = msm_otg_reboot_notify;
  1561. register_reboot_notifier(&motg->reboot);
  1562. pm_runtime_set_active(&pdev->dev);
  1563. pm_runtime_enable(&pdev->dev);
  1564. return 0;
  1565. disable_ldo:
  1566. msm_hsusb_ldo_init(motg, 0);
  1567. disable_vddcx:
  1568. msm_hsusb_init_vddcx(motg, 0);
  1569. disable_clks:
  1570. clk_disable_unprepare(motg->pclk);
  1571. clk_disable_unprepare(motg->clk);
  1572. if (!IS_ERR(motg->core_clk))
  1573. clk_disable_unprepare(motg->core_clk);
  1574. unregister_extcon:
  1575. extcon_unregister_notifier(motg->id.extcon,
  1576. EXTCON_USB_HOST, &motg->id.nb);
  1577. extcon_unregister_notifier(motg->vbus.extcon,
  1578. EXTCON_USB, &motg->vbus.nb);
  1579. return ret;
  1580. }
  1581. static int msm_otg_remove(struct platform_device *pdev)
  1582. {
  1583. struct msm_otg *motg = platform_get_drvdata(pdev);
  1584. struct usb_phy *phy = &motg->phy;
  1585. int cnt = 0;
  1586. if (phy->otg->host || phy->otg->gadget)
  1587. return -EBUSY;
  1588. unregister_reboot_notifier(&motg->reboot);
  1589. /*
  1590. * Ensure that D+/D- lines are routed to uB connector, so
  1591. * we could load bootloader/kernel at next reboot
  1592. */
  1593. gpiod_set_value_cansleep(motg->switch_gpio, 0);
  1594. extcon_unregister_notifier(motg->id.extcon, EXTCON_USB_HOST, &motg->id.nb);
  1595. extcon_unregister_notifier(motg->vbus.extcon, EXTCON_USB, &motg->vbus.nb);
  1596. msm_otg_debugfs_cleanup();
  1597. cancel_delayed_work_sync(&motg->chg_work);
  1598. cancel_work_sync(&motg->sm_work);
  1599. pm_runtime_resume(&pdev->dev);
  1600. device_init_wakeup(&pdev->dev, 0);
  1601. pm_runtime_disable(&pdev->dev);
  1602. usb_remove_phy(phy);
  1603. disable_irq(motg->irq);
  1604. /*
  1605. * Put PHY in low power mode.
  1606. */
  1607. ulpi_read(phy, 0x14);
  1608. ulpi_write(phy, 0x08, 0x09);
  1609. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  1610. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  1611. if (readl(USB_PORTSC) & PORTSC_PHCD)
  1612. break;
  1613. udelay(1);
  1614. cnt++;
  1615. }
  1616. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  1617. dev_err(phy->dev, "Unable to suspend PHY\n");
  1618. clk_disable_unprepare(motg->pclk);
  1619. clk_disable_unprepare(motg->clk);
  1620. if (!IS_ERR(motg->core_clk))
  1621. clk_disable_unprepare(motg->core_clk);
  1622. msm_hsusb_ldo_init(motg, 0);
  1623. pm_runtime_set_suspended(&pdev->dev);
  1624. return 0;
  1625. }
  1626. #ifdef CONFIG_PM
  1627. static int msm_otg_runtime_idle(struct device *dev)
  1628. {
  1629. struct msm_otg *motg = dev_get_drvdata(dev);
  1630. struct usb_otg *otg = motg->phy.otg;
  1631. dev_dbg(dev, "OTG runtime idle\n");
  1632. /*
  1633. * It is observed some times that a spurious interrupt
  1634. * comes when PHY is put into LPM immediately after PHY reset.
  1635. * This 1 sec delay also prevents entering into LPM immediately
  1636. * after asynchronous interrupt.
  1637. */
  1638. if (otg->state != OTG_STATE_UNDEFINED)
  1639. pm_schedule_suspend(dev, 1000);
  1640. return -EAGAIN;
  1641. }
  1642. static int msm_otg_runtime_suspend(struct device *dev)
  1643. {
  1644. struct msm_otg *motg = dev_get_drvdata(dev);
  1645. dev_dbg(dev, "OTG runtime suspend\n");
  1646. return msm_otg_suspend(motg);
  1647. }
  1648. static int msm_otg_runtime_resume(struct device *dev)
  1649. {
  1650. struct msm_otg *motg = dev_get_drvdata(dev);
  1651. dev_dbg(dev, "OTG runtime resume\n");
  1652. return msm_otg_resume(motg);
  1653. }
  1654. #endif
  1655. #ifdef CONFIG_PM_SLEEP
  1656. static int msm_otg_pm_suspend(struct device *dev)
  1657. {
  1658. struct msm_otg *motg = dev_get_drvdata(dev);
  1659. dev_dbg(dev, "OTG PM suspend\n");
  1660. return msm_otg_suspend(motg);
  1661. }
  1662. static int msm_otg_pm_resume(struct device *dev)
  1663. {
  1664. struct msm_otg *motg = dev_get_drvdata(dev);
  1665. int ret;
  1666. dev_dbg(dev, "OTG PM resume\n");
  1667. ret = msm_otg_resume(motg);
  1668. if (ret)
  1669. return ret;
  1670. /*
  1671. * Runtime PM Documentation recommends bringing the
  1672. * device to full powered state upon resume.
  1673. */
  1674. pm_runtime_disable(dev);
  1675. pm_runtime_set_active(dev);
  1676. pm_runtime_enable(dev);
  1677. return 0;
  1678. }
  1679. #endif
  1680. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  1681. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  1682. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  1683. msm_otg_runtime_idle)
  1684. };
  1685. static struct platform_driver msm_otg_driver = {
  1686. .probe = msm_otg_probe,
  1687. .remove = msm_otg_remove,
  1688. .driver = {
  1689. .name = DRIVER_NAME,
  1690. .pm = &msm_otg_dev_pm_ops,
  1691. .of_match_table = msm_otg_dt_match,
  1692. },
  1693. };
  1694. module_platform_driver(msm_otg_driver);
  1695. MODULE_LICENSE("GPL v2");
  1696. MODULE_DESCRIPTION("MSM USB transceiver driver");