msm_otg.c 36 KB

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