msm_otg.c 42 KB

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