msm_otg.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /* Copyright (c) 2009-2010, 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_start_host(struct otg_transceiver *otg, int on)
  348. {
  349. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  350. struct msm_otg_platform_data *pdata = motg->pdata;
  351. struct usb_hcd *hcd;
  352. if (!otg->host)
  353. return;
  354. hcd = bus_to_hcd(otg->host);
  355. if (on) {
  356. dev_dbg(otg->dev, "host on\n");
  357. if (pdata->vbus_power)
  358. pdata->vbus_power(1);
  359. /*
  360. * Some boards have a switch cotrolled by gpio
  361. * to enable/disable internal HUB. Enable internal
  362. * HUB before kicking the host.
  363. */
  364. if (pdata->setup_gpio)
  365. pdata->setup_gpio(OTG_STATE_A_HOST);
  366. #ifdef CONFIG_USB
  367. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  368. #endif
  369. } else {
  370. dev_dbg(otg->dev, "host off\n");
  371. #ifdef CONFIG_USB
  372. usb_remove_hcd(hcd);
  373. #endif
  374. if (pdata->setup_gpio)
  375. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  376. if (pdata->vbus_power)
  377. pdata->vbus_power(0);
  378. }
  379. }
  380. static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  381. {
  382. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  383. struct usb_hcd *hcd;
  384. /*
  385. * Fail host registration if this board can support
  386. * only peripheral configuration.
  387. */
  388. if (motg->pdata->mode == USB_PERIPHERAL) {
  389. dev_info(otg->dev, "Host mode is not supported\n");
  390. return -ENODEV;
  391. }
  392. if (!host) {
  393. if (otg->state == OTG_STATE_A_HOST) {
  394. pm_runtime_get_sync(otg->dev);
  395. msm_otg_start_host(otg, 0);
  396. otg->host = NULL;
  397. otg->state = OTG_STATE_UNDEFINED;
  398. schedule_work(&motg->sm_work);
  399. } else {
  400. otg->host = NULL;
  401. }
  402. return 0;
  403. }
  404. hcd = bus_to_hcd(host);
  405. hcd->power_budget = motg->pdata->power_budget;
  406. otg->host = host;
  407. dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
  408. /*
  409. * Kick the state machine work, if peripheral is not supported
  410. * or peripheral is already registered with us.
  411. */
  412. if (motg->pdata->mode == USB_HOST || otg->gadget) {
  413. pm_runtime_get_sync(otg->dev);
  414. schedule_work(&motg->sm_work);
  415. }
  416. return 0;
  417. }
  418. static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
  419. {
  420. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  421. struct msm_otg_platform_data *pdata = motg->pdata;
  422. if (!otg->gadget)
  423. return;
  424. if (on) {
  425. dev_dbg(otg->dev, "gadget on\n");
  426. /*
  427. * Some boards have a switch cotrolled by gpio
  428. * to enable/disable internal HUB. Disable internal
  429. * HUB before kicking the gadget.
  430. */
  431. if (pdata->setup_gpio)
  432. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  433. usb_gadget_vbus_connect(otg->gadget);
  434. } else {
  435. dev_dbg(otg->dev, "gadget off\n");
  436. usb_gadget_vbus_disconnect(otg->gadget);
  437. if (pdata->setup_gpio)
  438. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  439. }
  440. }
  441. static int msm_otg_set_peripheral(struct otg_transceiver *otg,
  442. struct usb_gadget *gadget)
  443. {
  444. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  445. /*
  446. * Fail peripheral registration if this board can support
  447. * only host configuration.
  448. */
  449. if (motg->pdata->mode == USB_HOST) {
  450. dev_info(otg->dev, "Peripheral mode is not supported\n");
  451. return -ENODEV;
  452. }
  453. if (!gadget) {
  454. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  455. pm_runtime_get_sync(otg->dev);
  456. msm_otg_start_peripheral(otg, 0);
  457. otg->gadget = NULL;
  458. otg->state = OTG_STATE_UNDEFINED;
  459. schedule_work(&motg->sm_work);
  460. } else {
  461. otg->gadget = NULL;
  462. }
  463. return 0;
  464. }
  465. otg->gadget = gadget;
  466. dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
  467. /*
  468. * Kick the state machine work, if host is not supported
  469. * or host is already registered with us.
  470. */
  471. if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
  472. pm_runtime_get_sync(otg->dev);
  473. schedule_work(&motg->sm_work);
  474. }
  475. return 0;
  476. }
  477. /*
  478. * We support OTG, Peripheral only and Host only configurations. In case
  479. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  480. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  481. * enabled when switch is controlled by user and default mode is supplied
  482. * by board file, which can be changed by userspace later.
  483. */
  484. static void msm_otg_init_sm(struct msm_otg *motg)
  485. {
  486. struct msm_otg_platform_data *pdata = motg->pdata;
  487. u32 otgsc = readl(USB_OTGSC);
  488. switch (pdata->mode) {
  489. case USB_OTG:
  490. if (pdata->otg_control == OTG_PHY_CONTROL) {
  491. if (otgsc & OTGSC_ID)
  492. set_bit(ID, &motg->inputs);
  493. else
  494. clear_bit(ID, &motg->inputs);
  495. if (otgsc & OTGSC_BSV)
  496. set_bit(B_SESS_VLD, &motg->inputs);
  497. else
  498. clear_bit(B_SESS_VLD, &motg->inputs);
  499. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  500. if (pdata->default_mode == USB_HOST) {
  501. clear_bit(ID, &motg->inputs);
  502. } else if (pdata->default_mode == USB_PERIPHERAL) {
  503. set_bit(ID, &motg->inputs);
  504. set_bit(B_SESS_VLD, &motg->inputs);
  505. } else {
  506. set_bit(ID, &motg->inputs);
  507. clear_bit(B_SESS_VLD, &motg->inputs);
  508. }
  509. }
  510. break;
  511. case USB_HOST:
  512. clear_bit(ID, &motg->inputs);
  513. break;
  514. case USB_PERIPHERAL:
  515. set_bit(ID, &motg->inputs);
  516. if (otgsc & OTGSC_BSV)
  517. set_bit(B_SESS_VLD, &motg->inputs);
  518. else
  519. clear_bit(B_SESS_VLD, &motg->inputs);
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. static void msm_otg_sm_work(struct work_struct *w)
  526. {
  527. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  528. struct otg_transceiver *otg = &motg->otg;
  529. switch (otg->state) {
  530. case OTG_STATE_UNDEFINED:
  531. dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
  532. msm_otg_reset(otg);
  533. msm_otg_init_sm(motg);
  534. otg->state = OTG_STATE_B_IDLE;
  535. /* FALL THROUGH */
  536. case OTG_STATE_B_IDLE:
  537. dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
  538. if (!test_bit(ID, &motg->inputs) && otg->host) {
  539. /* disable BSV bit */
  540. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  541. msm_otg_start_host(otg, 1);
  542. otg->state = OTG_STATE_A_HOST;
  543. } else if (test_bit(B_SESS_VLD, &motg->inputs) && otg->gadget) {
  544. msm_otg_start_peripheral(otg, 1);
  545. otg->state = OTG_STATE_B_PERIPHERAL;
  546. }
  547. pm_runtime_put_sync(otg->dev);
  548. break;
  549. case OTG_STATE_B_PERIPHERAL:
  550. dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
  551. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  552. !test_bit(ID, &motg->inputs)) {
  553. msm_otg_start_peripheral(otg, 0);
  554. otg->state = OTG_STATE_B_IDLE;
  555. msm_otg_reset(otg);
  556. schedule_work(w);
  557. }
  558. break;
  559. case OTG_STATE_A_HOST:
  560. dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
  561. if (test_bit(ID, &motg->inputs)) {
  562. msm_otg_start_host(otg, 0);
  563. otg->state = OTG_STATE_B_IDLE;
  564. msm_otg_reset(otg);
  565. schedule_work(w);
  566. }
  567. break;
  568. default:
  569. break;
  570. }
  571. }
  572. static irqreturn_t msm_otg_irq(int irq, void *data)
  573. {
  574. struct msm_otg *motg = data;
  575. struct otg_transceiver *otg = &motg->otg;
  576. u32 otgsc = 0;
  577. if (atomic_read(&motg->in_lpm)) {
  578. disable_irq_nosync(irq);
  579. motg->async_int = 1;
  580. pm_runtime_get(otg->dev);
  581. return IRQ_HANDLED;
  582. }
  583. otgsc = readl(USB_OTGSC);
  584. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  585. return IRQ_NONE;
  586. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  587. if (otgsc & OTGSC_ID)
  588. set_bit(ID, &motg->inputs);
  589. else
  590. clear_bit(ID, &motg->inputs);
  591. dev_dbg(otg->dev, "ID set/clear\n");
  592. pm_runtime_get_noresume(otg->dev);
  593. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  594. if (otgsc & OTGSC_BSV)
  595. set_bit(B_SESS_VLD, &motg->inputs);
  596. else
  597. clear_bit(B_SESS_VLD, &motg->inputs);
  598. dev_dbg(otg->dev, "BSV set/clear\n");
  599. pm_runtime_get_noresume(otg->dev);
  600. }
  601. writel(otgsc, USB_OTGSC);
  602. schedule_work(&motg->sm_work);
  603. return IRQ_HANDLED;
  604. }
  605. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  606. {
  607. struct msm_otg *motg = s->private;
  608. struct otg_transceiver *otg = &motg->otg;
  609. switch (otg->state) {
  610. case OTG_STATE_A_HOST:
  611. seq_printf(s, "host\n");
  612. break;
  613. case OTG_STATE_B_PERIPHERAL:
  614. seq_printf(s, "peripheral\n");
  615. break;
  616. default:
  617. seq_printf(s, "none\n");
  618. break;
  619. }
  620. return 0;
  621. }
  622. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  623. {
  624. return single_open(file, msm_otg_mode_show, inode->i_private);
  625. }
  626. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  627. size_t count, loff_t *ppos)
  628. {
  629. struct seq_file *s = file->private_data;
  630. struct msm_otg *motg = s->private;
  631. char buf[16];
  632. struct otg_transceiver *otg = &motg->otg;
  633. int status = count;
  634. enum usb_mode_type req_mode;
  635. memset(buf, 0x00, sizeof(buf));
  636. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  637. status = -EFAULT;
  638. goto out;
  639. }
  640. if (!strncmp(buf, "host", 4)) {
  641. req_mode = USB_HOST;
  642. } else if (!strncmp(buf, "peripheral", 10)) {
  643. req_mode = USB_PERIPHERAL;
  644. } else if (!strncmp(buf, "none", 4)) {
  645. req_mode = USB_NONE;
  646. } else {
  647. status = -EINVAL;
  648. goto out;
  649. }
  650. switch (req_mode) {
  651. case USB_NONE:
  652. switch (otg->state) {
  653. case OTG_STATE_A_HOST:
  654. case OTG_STATE_B_PERIPHERAL:
  655. set_bit(ID, &motg->inputs);
  656. clear_bit(B_SESS_VLD, &motg->inputs);
  657. break;
  658. default:
  659. goto out;
  660. }
  661. break;
  662. case USB_PERIPHERAL:
  663. switch (otg->state) {
  664. case OTG_STATE_B_IDLE:
  665. case OTG_STATE_A_HOST:
  666. set_bit(ID, &motg->inputs);
  667. set_bit(B_SESS_VLD, &motg->inputs);
  668. break;
  669. default:
  670. goto out;
  671. }
  672. break;
  673. case USB_HOST:
  674. switch (otg->state) {
  675. case OTG_STATE_B_IDLE:
  676. case OTG_STATE_B_PERIPHERAL:
  677. clear_bit(ID, &motg->inputs);
  678. break;
  679. default:
  680. goto out;
  681. }
  682. break;
  683. default:
  684. goto out;
  685. }
  686. pm_runtime_get_sync(otg->dev);
  687. schedule_work(&motg->sm_work);
  688. out:
  689. return status;
  690. }
  691. const struct file_operations msm_otg_mode_fops = {
  692. .open = msm_otg_mode_open,
  693. .read = seq_read,
  694. .write = msm_otg_mode_write,
  695. .llseek = seq_lseek,
  696. .release = single_release,
  697. };
  698. static struct dentry *msm_otg_dbg_root;
  699. static struct dentry *msm_otg_dbg_mode;
  700. static int msm_otg_debugfs_init(struct msm_otg *motg)
  701. {
  702. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  703. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  704. return -ENODEV;
  705. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  706. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  707. if (!msm_otg_dbg_mode) {
  708. debugfs_remove(msm_otg_dbg_root);
  709. msm_otg_dbg_root = NULL;
  710. return -ENODEV;
  711. }
  712. return 0;
  713. }
  714. static void msm_otg_debugfs_cleanup(void)
  715. {
  716. debugfs_remove(msm_otg_dbg_mode);
  717. debugfs_remove(msm_otg_dbg_root);
  718. }
  719. static int __init msm_otg_probe(struct platform_device *pdev)
  720. {
  721. int ret = 0;
  722. struct resource *res;
  723. struct msm_otg *motg;
  724. struct otg_transceiver *otg;
  725. dev_info(&pdev->dev, "msm_otg probe\n");
  726. if (!pdev->dev.platform_data) {
  727. dev_err(&pdev->dev, "No platform data given. Bailing out\n");
  728. return -ENODEV;
  729. }
  730. motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
  731. if (!motg) {
  732. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  733. return -ENOMEM;
  734. }
  735. motg->pdata = pdev->dev.platform_data;
  736. otg = &motg->otg;
  737. otg->dev = &pdev->dev;
  738. motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
  739. if (IS_ERR(motg->phy_reset_clk)) {
  740. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  741. ret = PTR_ERR(motg->phy_reset_clk);
  742. goto free_motg;
  743. }
  744. motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
  745. if (IS_ERR(motg->clk)) {
  746. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  747. ret = PTR_ERR(motg->clk);
  748. goto put_phy_reset_clk;
  749. }
  750. clk_set_rate(motg->clk, 60000000);
  751. /*
  752. * If USB Core is running its protocol engine based on CORE CLK,
  753. * CORE CLK must be running at >55Mhz for correct HSUSB
  754. * operation and USB core cannot tolerate frequency changes on
  755. * CORE CLK. For such USB cores, vote for maximum clk frequency
  756. * on pclk source
  757. */
  758. if (motg->pdata->pclk_src_name) {
  759. motg->pclk_src = clk_get(&pdev->dev,
  760. motg->pdata->pclk_src_name);
  761. if (IS_ERR(motg->pclk_src))
  762. goto put_clk;
  763. clk_set_rate(motg->pclk_src, INT_MAX);
  764. clk_enable(motg->pclk_src);
  765. } else
  766. motg->pclk_src = ERR_PTR(-ENOENT);
  767. motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
  768. if (IS_ERR(motg->pclk)) {
  769. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  770. ret = PTR_ERR(motg->pclk);
  771. goto put_pclk_src;
  772. }
  773. /*
  774. * USB core clock is not present on all MSM chips. This
  775. * clock is introduced to remove the dependency on AXI
  776. * bus frequency.
  777. */
  778. motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
  779. if (IS_ERR(motg->core_clk))
  780. motg->core_clk = NULL;
  781. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  782. if (!res) {
  783. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  784. ret = -ENODEV;
  785. goto put_core_clk;
  786. }
  787. motg->regs = ioremap(res->start, resource_size(res));
  788. if (!motg->regs) {
  789. dev_err(&pdev->dev, "ioremap failed\n");
  790. ret = -ENOMEM;
  791. goto put_core_clk;
  792. }
  793. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  794. motg->irq = platform_get_irq(pdev, 0);
  795. if (!motg->irq) {
  796. dev_err(&pdev->dev, "platform_get_irq failed\n");
  797. ret = -ENODEV;
  798. goto free_regs;
  799. }
  800. clk_enable(motg->clk);
  801. clk_enable(motg->pclk);
  802. if (motg->core_clk)
  803. clk_enable(motg->core_clk);
  804. writel(0, USB_USBINTR);
  805. writel(0, USB_OTGSC);
  806. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  807. ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
  808. "msm_otg", motg);
  809. if (ret) {
  810. dev_err(&pdev->dev, "request irq failed\n");
  811. goto disable_clks;
  812. }
  813. otg->init = msm_otg_reset;
  814. otg->set_host = msm_otg_set_host;
  815. otg->set_peripheral = msm_otg_set_peripheral;
  816. otg->io_ops = &msm_otg_io_ops;
  817. ret = otg_set_transceiver(&motg->otg);
  818. if (ret) {
  819. dev_err(&pdev->dev, "otg_set_transceiver failed\n");
  820. goto free_irq;
  821. }
  822. platform_set_drvdata(pdev, motg);
  823. device_init_wakeup(&pdev->dev, 1);
  824. if (motg->pdata->mode == USB_OTG &&
  825. motg->pdata->otg_control == OTG_USER_CONTROL) {
  826. ret = msm_otg_debugfs_init(motg);
  827. if (ret)
  828. dev_dbg(&pdev->dev, "mode debugfs file is"
  829. "not available\n");
  830. }
  831. pm_runtime_set_active(&pdev->dev);
  832. pm_runtime_enable(&pdev->dev);
  833. return 0;
  834. free_irq:
  835. free_irq(motg->irq, motg);
  836. disable_clks:
  837. clk_disable(motg->pclk);
  838. clk_disable(motg->clk);
  839. free_regs:
  840. iounmap(motg->regs);
  841. put_core_clk:
  842. if (motg->core_clk)
  843. clk_put(motg->core_clk);
  844. clk_put(motg->pclk);
  845. put_pclk_src:
  846. if (!IS_ERR(motg->pclk_src)) {
  847. clk_disable(motg->pclk_src);
  848. clk_put(motg->pclk_src);
  849. }
  850. put_clk:
  851. clk_put(motg->clk);
  852. put_phy_reset_clk:
  853. clk_put(motg->phy_reset_clk);
  854. free_motg:
  855. kfree(motg);
  856. return ret;
  857. }
  858. static int __devexit msm_otg_remove(struct platform_device *pdev)
  859. {
  860. struct msm_otg *motg = platform_get_drvdata(pdev);
  861. struct otg_transceiver *otg = &motg->otg;
  862. int cnt = 0;
  863. if (otg->host || otg->gadget)
  864. return -EBUSY;
  865. msm_otg_debugfs_cleanup();
  866. cancel_work_sync(&motg->sm_work);
  867. pm_runtime_resume(&pdev->dev);
  868. device_init_wakeup(&pdev->dev, 0);
  869. pm_runtime_disable(&pdev->dev);
  870. otg_set_transceiver(NULL);
  871. free_irq(motg->irq, motg);
  872. /*
  873. * Put PHY in low power mode.
  874. */
  875. ulpi_read(otg, 0x14);
  876. ulpi_write(otg, 0x08, 0x09);
  877. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  878. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  879. if (readl(USB_PORTSC) & PORTSC_PHCD)
  880. break;
  881. udelay(1);
  882. cnt++;
  883. }
  884. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  885. dev_err(otg->dev, "Unable to suspend PHY\n");
  886. clk_disable(motg->pclk);
  887. clk_disable(motg->clk);
  888. if (motg->core_clk)
  889. clk_disable(motg->core_clk);
  890. if (!IS_ERR(motg->pclk_src)) {
  891. clk_disable(motg->pclk_src);
  892. clk_put(motg->pclk_src);
  893. }
  894. iounmap(motg->regs);
  895. pm_runtime_set_suspended(&pdev->dev);
  896. clk_put(motg->phy_reset_clk);
  897. clk_put(motg->pclk);
  898. clk_put(motg->clk);
  899. if (motg->core_clk)
  900. clk_put(motg->core_clk);
  901. kfree(motg);
  902. return 0;
  903. }
  904. #ifdef CONFIG_PM_RUNTIME
  905. static int msm_otg_runtime_idle(struct device *dev)
  906. {
  907. struct msm_otg *motg = dev_get_drvdata(dev);
  908. struct otg_transceiver *otg = &motg->otg;
  909. dev_dbg(dev, "OTG runtime idle\n");
  910. /*
  911. * It is observed some times that a spurious interrupt
  912. * comes when PHY is put into LPM immediately after PHY reset.
  913. * This 1 sec delay also prevents entering into LPM immediately
  914. * after asynchronous interrupt.
  915. */
  916. if (otg->state != OTG_STATE_UNDEFINED)
  917. pm_schedule_suspend(dev, 1000);
  918. return -EAGAIN;
  919. }
  920. static int msm_otg_runtime_suspend(struct device *dev)
  921. {
  922. struct msm_otg *motg = dev_get_drvdata(dev);
  923. dev_dbg(dev, "OTG runtime suspend\n");
  924. return msm_otg_suspend(motg);
  925. }
  926. static int msm_otg_runtime_resume(struct device *dev)
  927. {
  928. struct msm_otg *motg = dev_get_drvdata(dev);
  929. dev_dbg(dev, "OTG runtime resume\n");
  930. return msm_otg_resume(motg);
  931. }
  932. #endif
  933. #ifdef CONFIG_PM_SLEEP
  934. static int msm_otg_pm_suspend(struct device *dev)
  935. {
  936. struct msm_otg *motg = dev_get_drvdata(dev);
  937. dev_dbg(dev, "OTG PM suspend\n");
  938. return msm_otg_suspend(motg);
  939. }
  940. static int msm_otg_pm_resume(struct device *dev)
  941. {
  942. struct msm_otg *motg = dev_get_drvdata(dev);
  943. int ret;
  944. dev_dbg(dev, "OTG PM resume\n");
  945. ret = msm_otg_resume(motg);
  946. if (ret)
  947. return ret;
  948. /*
  949. * Runtime PM Documentation recommends bringing the
  950. * device to full powered state upon resume.
  951. */
  952. pm_runtime_disable(dev);
  953. pm_runtime_set_active(dev);
  954. pm_runtime_enable(dev);
  955. return 0;
  956. }
  957. #endif
  958. #ifdef CONFIG_PM
  959. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  960. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  961. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  962. msm_otg_runtime_idle)
  963. };
  964. #endif
  965. static struct platform_driver msm_otg_driver = {
  966. .remove = __devexit_p(msm_otg_remove),
  967. .driver = {
  968. .name = DRIVER_NAME,
  969. .owner = THIS_MODULE,
  970. #ifdef CONFIG_PM
  971. .pm = &msm_otg_dev_pm_ops,
  972. #endif
  973. },
  974. };
  975. static int __init msm_otg_init(void)
  976. {
  977. return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
  978. }
  979. static void __exit msm_otg_exit(void)
  980. {
  981. platform_driver_unregister(&msm_otg_driver);
  982. }
  983. module_init(msm_otg_init);
  984. module_exit(msm_otg_exit);
  985. MODULE_LICENSE("GPL v2");
  986. MODULE_DESCRIPTION("MSM USB transceiver driver");