phy-msm-usb.c 44 KB

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