phy-msm-usb.c 47 KB

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