msm72k_otg.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. static int msm_otg_suspend(struct msm_otg *motg)
  220. {
  221. struct otg_transceiver *otg = &motg->otg;
  222. struct usb_bus *bus = otg->host;
  223. struct msm_otg_platform_data *pdata = motg->pdata;
  224. int cnt = 0;
  225. if (atomic_read(&motg->in_lpm))
  226. return 0;
  227. disable_irq(motg->irq);
  228. /*
  229. * Interrupt Latch Register auto-clear feature is not present
  230. * in all PHY versions. Latch register is clear on read type.
  231. * Clear latch register to avoid spurious wakeup from
  232. * low power mode (LPM).
  233. */
  234. ulpi_read(otg, 0x14);
  235. /*
  236. * PHY comparators are disabled when PHY enters into low power
  237. * mode (LPM). Keep PHY comparators ON in LPM only when we expect
  238. * VBUS/Id notifications from USB PHY. Otherwise turn off USB
  239. * PHY comparators. This save significant amount of power.
  240. */
  241. if (pdata->otg_control == OTG_PHY_CONTROL)
  242. ulpi_write(otg, 0x01, 0x30);
  243. /*
  244. * PLL is not turned off when PHY enters into low power mode (LPM).
  245. * Disable PLL for maximum power savings.
  246. */
  247. ulpi_write(otg, 0x08, 0x09);
  248. /*
  249. * PHY may take some time or even fail to enter into low power
  250. * mode (LPM). Hence poll for 500 msec and reset the PHY and link
  251. * in failure case.
  252. */
  253. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  254. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  255. if (readl(USB_PORTSC) & PORTSC_PHCD)
  256. break;
  257. udelay(1);
  258. cnt++;
  259. }
  260. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
  261. dev_err(otg->dev, "Unable to suspend PHY\n");
  262. msm_otg_reset(otg);
  263. enable_irq(motg->irq);
  264. return -ETIMEDOUT;
  265. }
  266. /*
  267. * PHY has capability to generate interrupt asynchronously in low
  268. * power mode (LPM). This interrupt is level triggered. So USB IRQ
  269. * line must be disabled till async interrupt enable bit is cleared
  270. * in USBCMD register. Assert STP (ULPI interface STOP signal) to
  271. * block data communication from PHY.
  272. */
  273. writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
  274. clk_disable(motg->pclk);
  275. clk_disable(motg->clk);
  276. if (motg->core_clk)
  277. clk_disable(motg->core_clk);
  278. if (device_may_wakeup(otg->dev))
  279. enable_irq_wake(motg->irq);
  280. if (bus)
  281. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  282. atomic_set(&motg->in_lpm, 1);
  283. enable_irq(motg->irq);
  284. dev_info(otg->dev, "USB in low power mode\n");
  285. return 0;
  286. }
  287. #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
  288. static int msm_otg_resume(struct msm_otg *motg)
  289. {
  290. struct otg_transceiver *otg = &motg->otg;
  291. struct usb_bus *bus = otg->host;
  292. int cnt = 0;
  293. unsigned temp;
  294. if (!atomic_read(&motg->in_lpm))
  295. return 0;
  296. clk_enable(motg->pclk);
  297. clk_enable(motg->clk);
  298. if (motg->core_clk)
  299. clk_enable(motg->core_clk);
  300. temp = readl(USB_USBCMD);
  301. temp &= ~ASYNC_INTR_CTRL;
  302. temp &= ~ULPI_STP_CTRL;
  303. writel(temp, USB_USBCMD);
  304. /*
  305. * PHY comes out of low power mode (LPM) in case of wakeup
  306. * from asynchronous interrupt.
  307. */
  308. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  309. goto skip_phy_resume;
  310. writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
  311. while (cnt < PHY_RESUME_TIMEOUT_USEC) {
  312. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  313. break;
  314. udelay(1);
  315. cnt++;
  316. }
  317. if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
  318. /*
  319. * This is a fatal error. Reset the link and
  320. * PHY. USB state can not be restored. Re-insertion
  321. * of USB cable is the only way to get USB working.
  322. */
  323. dev_err(otg->dev, "Unable to resume USB."
  324. "Re-plugin the cable\n");
  325. msm_otg_reset(otg);
  326. }
  327. skip_phy_resume:
  328. if (device_may_wakeup(otg->dev))
  329. disable_irq_wake(motg->irq);
  330. if (bus)
  331. set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  332. if (motg->async_int) {
  333. motg->async_int = 0;
  334. pm_runtime_put(otg->dev);
  335. enable_irq(motg->irq);
  336. }
  337. atomic_set(&motg->in_lpm, 0);
  338. dev_info(otg->dev, "USB exited from low power mode\n");
  339. return 0;
  340. }
  341. static void msm_otg_start_host(struct otg_transceiver *otg, int on)
  342. {
  343. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  344. struct msm_otg_platform_data *pdata = motg->pdata;
  345. struct usb_hcd *hcd;
  346. if (!otg->host)
  347. return;
  348. hcd = bus_to_hcd(otg->host);
  349. if (on) {
  350. dev_dbg(otg->dev, "host on\n");
  351. if (pdata->vbus_power)
  352. pdata->vbus_power(1);
  353. /*
  354. * Some boards have a switch cotrolled by gpio
  355. * to enable/disable internal HUB. Enable internal
  356. * HUB before kicking the host.
  357. */
  358. if (pdata->setup_gpio)
  359. pdata->setup_gpio(OTG_STATE_A_HOST);
  360. #ifdef CONFIG_USB
  361. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  362. #endif
  363. } else {
  364. dev_dbg(otg->dev, "host off\n");
  365. #ifdef CONFIG_USB
  366. usb_remove_hcd(hcd);
  367. #endif
  368. if (pdata->setup_gpio)
  369. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  370. if (pdata->vbus_power)
  371. pdata->vbus_power(0);
  372. }
  373. }
  374. static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
  375. {
  376. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  377. struct usb_hcd *hcd;
  378. /*
  379. * Fail host registration if this board can support
  380. * only peripheral configuration.
  381. */
  382. if (motg->pdata->mode == USB_PERIPHERAL) {
  383. dev_info(otg->dev, "Host mode is not supported\n");
  384. return -ENODEV;
  385. }
  386. if (!host) {
  387. if (otg->state == OTG_STATE_A_HOST) {
  388. pm_runtime_get_sync(otg->dev);
  389. msm_otg_start_host(otg, 0);
  390. otg->host = NULL;
  391. otg->state = OTG_STATE_UNDEFINED;
  392. schedule_work(&motg->sm_work);
  393. } else {
  394. otg->host = NULL;
  395. }
  396. return 0;
  397. }
  398. hcd = bus_to_hcd(host);
  399. hcd->power_budget = motg->pdata->power_budget;
  400. otg->host = host;
  401. dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
  402. /*
  403. * Kick the state machine work, if peripheral is not supported
  404. * or peripheral is already registered with us.
  405. */
  406. if (motg->pdata->mode == USB_HOST || otg->gadget) {
  407. pm_runtime_get_sync(otg->dev);
  408. schedule_work(&motg->sm_work);
  409. }
  410. return 0;
  411. }
  412. static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
  413. {
  414. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  415. struct msm_otg_platform_data *pdata = motg->pdata;
  416. if (!otg->gadget)
  417. return;
  418. if (on) {
  419. dev_dbg(otg->dev, "gadget on\n");
  420. /*
  421. * Some boards have a switch cotrolled by gpio
  422. * to enable/disable internal HUB. Disable internal
  423. * HUB before kicking the gadget.
  424. */
  425. if (pdata->setup_gpio)
  426. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  427. usb_gadget_vbus_connect(otg->gadget);
  428. } else {
  429. dev_dbg(otg->dev, "gadget off\n");
  430. usb_gadget_vbus_disconnect(otg->gadget);
  431. if (pdata->setup_gpio)
  432. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  433. }
  434. }
  435. static int msm_otg_set_peripheral(struct otg_transceiver *otg,
  436. struct usb_gadget *gadget)
  437. {
  438. struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
  439. /*
  440. * Fail peripheral registration if this board can support
  441. * only host configuration.
  442. */
  443. if (motg->pdata->mode == USB_HOST) {
  444. dev_info(otg->dev, "Peripheral mode is not supported\n");
  445. return -ENODEV;
  446. }
  447. if (!gadget) {
  448. if (otg->state == OTG_STATE_B_PERIPHERAL) {
  449. pm_runtime_get_sync(otg->dev);
  450. msm_otg_start_peripheral(otg, 0);
  451. otg->gadget = NULL;
  452. otg->state = OTG_STATE_UNDEFINED;
  453. schedule_work(&motg->sm_work);
  454. } else {
  455. otg->gadget = NULL;
  456. }
  457. return 0;
  458. }
  459. otg->gadget = gadget;
  460. dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
  461. /*
  462. * Kick the state machine work, if host is not supported
  463. * or host is already registered with us.
  464. */
  465. if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
  466. pm_runtime_get_sync(otg->dev);
  467. schedule_work(&motg->sm_work);
  468. }
  469. return 0;
  470. }
  471. /*
  472. * We support OTG, Peripheral only and Host only configurations. In case
  473. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  474. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  475. * enabled when switch is controlled by user and default mode is supplied
  476. * by board file, which can be changed by userspace later.
  477. */
  478. static void msm_otg_init_sm(struct msm_otg *motg)
  479. {
  480. struct msm_otg_platform_data *pdata = motg->pdata;
  481. u32 otgsc = readl(USB_OTGSC);
  482. switch (pdata->mode) {
  483. case USB_OTG:
  484. if (pdata->otg_control == OTG_PHY_CONTROL) {
  485. if (otgsc & OTGSC_ID)
  486. set_bit(ID, &motg->inputs);
  487. else
  488. clear_bit(ID, &motg->inputs);
  489. if (otgsc & OTGSC_BSV)
  490. set_bit(B_SESS_VLD, &motg->inputs);
  491. else
  492. clear_bit(B_SESS_VLD, &motg->inputs);
  493. } else if (pdata->otg_control == OTG_USER_CONTROL) {
  494. if (pdata->default_mode == USB_HOST) {
  495. clear_bit(ID, &motg->inputs);
  496. } else if (pdata->default_mode == USB_PERIPHERAL) {
  497. set_bit(ID, &motg->inputs);
  498. set_bit(B_SESS_VLD, &motg->inputs);
  499. } else {
  500. set_bit(ID, &motg->inputs);
  501. clear_bit(B_SESS_VLD, &motg->inputs);
  502. }
  503. }
  504. break;
  505. case USB_HOST:
  506. clear_bit(ID, &motg->inputs);
  507. break;
  508. case USB_PERIPHERAL:
  509. set_bit(ID, &motg->inputs);
  510. if (otgsc & OTGSC_BSV)
  511. set_bit(B_SESS_VLD, &motg->inputs);
  512. else
  513. clear_bit(B_SESS_VLD, &motg->inputs);
  514. break;
  515. default:
  516. break;
  517. }
  518. }
  519. static void msm_otg_sm_work(struct work_struct *w)
  520. {
  521. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  522. struct otg_transceiver *otg = &motg->otg;
  523. switch (otg->state) {
  524. case OTG_STATE_UNDEFINED:
  525. dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
  526. msm_otg_reset(otg);
  527. msm_otg_init_sm(motg);
  528. otg->state = OTG_STATE_B_IDLE;
  529. /* FALL THROUGH */
  530. case OTG_STATE_B_IDLE:
  531. dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
  532. if (!test_bit(ID, &motg->inputs) && otg->host) {
  533. /* disable BSV bit */
  534. writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
  535. msm_otg_start_host(otg, 1);
  536. otg->state = OTG_STATE_A_HOST;
  537. } else if (test_bit(B_SESS_VLD, &motg->inputs) && otg->gadget) {
  538. msm_otg_start_peripheral(otg, 1);
  539. otg->state = OTG_STATE_B_PERIPHERAL;
  540. }
  541. pm_runtime_put_sync(otg->dev);
  542. break;
  543. case OTG_STATE_B_PERIPHERAL:
  544. dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
  545. if (!test_bit(B_SESS_VLD, &motg->inputs) ||
  546. !test_bit(ID, &motg->inputs)) {
  547. msm_otg_start_peripheral(otg, 0);
  548. otg->state = OTG_STATE_B_IDLE;
  549. msm_otg_reset(otg);
  550. schedule_work(w);
  551. }
  552. break;
  553. case OTG_STATE_A_HOST:
  554. dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
  555. if (test_bit(ID, &motg->inputs)) {
  556. msm_otg_start_host(otg, 0);
  557. otg->state = OTG_STATE_B_IDLE;
  558. msm_otg_reset(otg);
  559. schedule_work(w);
  560. }
  561. break;
  562. default:
  563. break;
  564. }
  565. }
  566. static irqreturn_t msm_otg_irq(int irq, void *data)
  567. {
  568. struct msm_otg *motg = data;
  569. struct otg_transceiver *otg = &motg->otg;
  570. u32 otgsc = 0;
  571. if (atomic_read(&motg->in_lpm)) {
  572. disable_irq_nosync(irq);
  573. motg->async_int = 1;
  574. pm_runtime_get(otg->dev);
  575. return IRQ_HANDLED;
  576. }
  577. otgsc = readl(USB_OTGSC);
  578. if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
  579. return IRQ_NONE;
  580. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  581. if (otgsc & OTGSC_ID)
  582. set_bit(ID, &motg->inputs);
  583. else
  584. clear_bit(ID, &motg->inputs);
  585. dev_dbg(otg->dev, "ID set/clear\n");
  586. pm_runtime_get_noresume(otg->dev);
  587. } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
  588. if (otgsc & OTGSC_BSV)
  589. set_bit(B_SESS_VLD, &motg->inputs);
  590. else
  591. clear_bit(B_SESS_VLD, &motg->inputs);
  592. dev_dbg(otg->dev, "BSV set/clear\n");
  593. pm_runtime_get_noresume(otg->dev);
  594. }
  595. writel(otgsc, USB_OTGSC);
  596. schedule_work(&motg->sm_work);
  597. return IRQ_HANDLED;
  598. }
  599. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  600. {
  601. struct msm_otg *motg = s->private;
  602. struct otg_transceiver *otg = &motg->otg;
  603. switch (otg->state) {
  604. case OTG_STATE_A_HOST:
  605. seq_printf(s, "host\n");
  606. break;
  607. case OTG_STATE_B_PERIPHERAL:
  608. seq_printf(s, "peripheral\n");
  609. break;
  610. default:
  611. seq_printf(s, "none\n");
  612. break;
  613. }
  614. return 0;
  615. }
  616. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  617. {
  618. return single_open(file, msm_otg_mode_show, inode->i_private);
  619. }
  620. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  621. size_t count, loff_t *ppos)
  622. {
  623. struct msm_otg *motg = file->private_data;
  624. char buf[16];
  625. struct otg_transceiver *otg = &motg->otg;
  626. int status = count;
  627. enum usb_mode_type req_mode;
  628. memset(buf, 0x00, sizeof(buf));
  629. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  630. status = -EFAULT;
  631. goto out;
  632. }
  633. if (!strncmp(buf, "host", 4)) {
  634. req_mode = USB_HOST;
  635. } else if (!strncmp(buf, "peripheral", 10)) {
  636. req_mode = USB_PERIPHERAL;
  637. } else if (!strncmp(buf, "none", 4)) {
  638. req_mode = USB_NONE;
  639. } else {
  640. status = -EINVAL;
  641. goto out;
  642. }
  643. switch (req_mode) {
  644. case USB_NONE:
  645. switch (otg->state) {
  646. case OTG_STATE_A_HOST:
  647. case OTG_STATE_B_PERIPHERAL:
  648. set_bit(ID, &motg->inputs);
  649. clear_bit(B_SESS_VLD, &motg->inputs);
  650. break;
  651. default:
  652. goto out;
  653. }
  654. break;
  655. case USB_PERIPHERAL:
  656. switch (otg->state) {
  657. case OTG_STATE_B_IDLE:
  658. case OTG_STATE_A_HOST:
  659. set_bit(ID, &motg->inputs);
  660. set_bit(B_SESS_VLD, &motg->inputs);
  661. break;
  662. default:
  663. goto out;
  664. }
  665. break;
  666. case USB_HOST:
  667. switch (otg->state) {
  668. case OTG_STATE_B_IDLE:
  669. case OTG_STATE_B_PERIPHERAL:
  670. clear_bit(ID, &motg->inputs);
  671. break;
  672. default:
  673. goto out;
  674. }
  675. break;
  676. default:
  677. goto out;
  678. }
  679. pm_runtime_get_sync(otg->dev);
  680. schedule_work(&motg->sm_work);
  681. out:
  682. return status;
  683. }
  684. const struct file_operations msm_otg_mode_fops = {
  685. .open = msm_otg_mode_open,
  686. .read = seq_read,
  687. .write = msm_otg_mode_write,
  688. .llseek = seq_lseek,
  689. .release = single_release,
  690. };
  691. static struct dentry *msm_otg_dbg_root;
  692. static struct dentry *msm_otg_dbg_mode;
  693. static int msm_otg_debugfs_init(struct msm_otg *motg)
  694. {
  695. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  696. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  697. return -ENODEV;
  698. msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
  699. msm_otg_dbg_root, motg, &msm_otg_mode_fops);
  700. if (!msm_otg_dbg_mode) {
  701. debugfs_remove(msm_otg_dbg_root);
  702. msm_otg_dbg_root = NULL;
  703. return -ENODEV;
  704. }
  705. return 0;
  706. }
  707. static void msm_otg_debugfs_cleanup(void)
  708. {
  709. debugfs_remove(msm_otg_dbg_mode);
  710. debugfs_remove(msm_otg_dbg_root);
  711. }
  712. static int __init msm_otg_probe(struct platform_device *pdev)
  713. {
  714. int ret = 0;
  715. struct resource *res;
  716. struct msm_otg *motg;
  717. struct otg_transceiver *otg;
  718. dev_info(&pdev->dev, "msm_otg probe\n");
  719. if (!pdev->dev.platform_data) {
  720. dev_err(&pdev->dev, "No platform data given. Bailing out\n");
  721. return -ENODEV;
  722. }
  723. motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
  724. if (!motg) {
  725. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  726. return -ENOMEM;
  727. }
  728. motg->pdata = pdev->dev.platform_data;
  729. otg = &motg->otg;
  730. otg->dev = &pdev->dev;
  731. motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
  732. if (IS_ERR(motg->phy_reset_clk)) {
  733. dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
  734. ret = PTR_ERR(motg->phy_reset_clk);
  735. goto free_motg;
  736. }
  737. motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
  738. if (IS_ERR(motg->clk)) {
  739. dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
  740. ret = PTR_ERR(motg->clk);
  741. goto put_phy_reset_clk;
  742. }
  743. motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
  744. if (IS_ERR(motg->pclk)) {
  745. dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
  746. ret = PTR_ERR(motg->pclk);
  747. goto put_clk;
  748. }
  749. /*
  750. * USB core clock is not present on all MSM chips. This
  751. * clock is introduced to remove the dependency on AXI
  752. * bus frequency.
  753. */
  754. motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
  755. if (IS_ERR(motg->core_clk))
  756. motg->core_clk = NULL;
  757. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  758. if (!res) {
  759. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  760. ret = -ENODEV;
  761. goto put_core_clk;
  762. }
  763. motg->regs = ioremap(res->start, resource_size(res));
  764. if (!motg->regs) {
  765. dev_err(&pdev->dev, "ioremap failed\n");
  766. ret = -ENOMEM;
  767. goto put_core_clk;
  768. }
  769. dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
  770. motg->irq = platform_get_irq(pdev, 0);
  771. if (!motg->irq) {
  772. dev_err(&pdev->dev, "platform_get_irq failed\n");
  773. ret = -ENODEV;
  774. goto free_regs;
  775. }
  776. clk_enable(motg->clk);
  777. clk_enable(motg->pclk);
  778. if (motg->core_clk)
  779. clk_enable(motg->core_clk);
  780. writel(0, USB_USBINTR);
  781. writel(0, USB_OTGSC);
  782. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  783. ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
  784. "msm_otg", motg);
  785. if (ret) {
  786. dev_err(&pdev->dev, "request irq failed\n");
  787. goto disable_clks;
  788. }
  789. otg->init = msm_otg_reset;
  790. otg->set_host = msm_otg_set_host;
  791. otg->set_peripheral = msm_otg_set_peripheral;
  792. otg->io_ops = &msm_otg_io_ops;
  793. ret = otg_set_transceiver(&motg->otg);
  794. if (ret) {
  795. dev_err(&pdev->dev, "otg_set_transceiver failed\n");
  796. goto free_irq;
  797. }
  798. platform_set_drvdata(pdev, motg);
  799. device_init_wakeup(&pdev->dev, 1);
  800. if (motg->pdata->mode == USB_OTG &&
  801. motg->pdata->otg_control == OTG_USER_CONTROL) {
  802. ret = msm_otg_debugfs_init(motg);
  803. if (ret)
  804. dev_dbg(&pdev->dev, "mode debugfs file is"
  805. "not available\n");
  806. }
  807. pm_runtime_set_active(&pdev->dev);
  808. pm_runtime_enable(&pdev->dev);
  809. return 0;
  810. free_irq:
  811. free_irq(motg->irq, motg);
  812. disable_clks:
  813. clk_disable(motg->pclk);
  814. clk_disable(motg->clk);
  815. free_regs:
  816. iounmap(motg->regs);
  817. put_core_clk:
  818. if (motg->core_clk)
  819. clk_put(motg->core_clk);
  820. clk_put(motg->pclk);
  821. put_clk:
  822. clk_put(motg->clk);
  823. put_phy_reset_clk:
  824. clk_put(motg->phy_reset_clk);
  825. free_motg:
  826. kfree(motg);
  827. return ret;
  828. }
  829. static int __devexit msm_otg_remove(struct platform_device *pdev)
  830. {
  831. struct msm_otg *motg = platform_get_drvdata(pdev);
  832. struct otg_transceiver *otg = &motg->otg;
  833. int cnt = 0;
  834. if (otg->host || otg->gadget)
  835. return -EBUSY;
  836. msm_otg_debugfs_cleanup();
  837. cancel_work_sync(&motg->sm_work);
  838. msm_otg_resume(motg);
  839. device_init_wakeup(&pdev->dev, 0);
  840. pm_runtime_disable(&pdev->dev);
  841. otg_set_transceiver(NULL);
  842. free_irq(motg->irq, motg);
  843. /*
  844. * Put PHY in low power mode.
  845. */
  846. ulpi_read(otg, 0x14);
  847. ulpi_write(otg, 0x08, 0x09);
  848. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  849. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  850. if (readl(USB_PORTSC) & PORTSC_PHCD)
  851. break;
  852. udelay(1);
  853. cnt++;
  854. }
  855. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  856. dev_err(otg->dev, "Unable to suspend PHY\n");
  857. clk_disable(motg->pclk);
  858. clk_disable(motg->clk);
  859. if (motg->core_clk)
  860. clk_disable(motg->core_clk);
  861. iounmap(motg->regs);
  862. pm_runtime_set_suspended(&pdev->dev);
  863. clk_put(motg->phy_reset_clk);
  864. clk_put(motg->pclk);
  865. clk_put(motg->clk);
  866. if (motg->core_clk)
  867. clk_put(motg->core_clk);
  868. kfree(motg);
  869. return 0;
  870. }
  871. #ifdef CONFIG_PM_RUNTIME
  872. static int msm_otg_runtime_idle(struct device *dev)
  873. {
  874. struct msm_otg *motg = dev_get_drvdata(dev);
  875. struct otg_transceiver *otg = &motg->otg;
  876. dev_dbg(dev, "OTG runtime idle\n");
  877. /*
  878. * It is observed some times that a spurious interrupt
  879. * comes when PHY is put into LPM immediately after PHY reset.
  880. * This 1 sec delay also prevents entering into LPM immediately
  881. * after asynchronous interrupt.
  882. */
  883. if (otg->state != OTG_STATE_UNDEFINED)
  884. pm_schedule_suspend(dev, 1000);
  885. return -EAGAIN;
  886. }
  887. static int msm_otg_runtime_suspend(struct device *dev)
  888. {
  889. struct msm_otg *motg = dev_get_drvdata(dev);
  890. dev_dbg(dev, "OTG runtime suspend\n");
  891. return msm_otg_suspend(motg);
  892. }
  893. static int msm_otg_runtime_resume(struct device *dev)
  894. {
  895. struct msm_otg *motg = dev_get_drvdata(dev);
  896. dev_dbg(dev, "OTG runtime resume\n");
  897. return msm_otg_resume(motg);
  898. }
  899. #else
  900. #define msm_otg_runtime_idle NULL
  901. #define msm_otg_runtime_suspend NULL
  902. #define msm_otg_runtime_resume NULL
  903. #endif
  904. #ifdef CONFIG_PM
  905. static int msm_otg_pm_suspend(struct device *dev)
  906. {
  907. struct msm_otg *motg = dev_get_drvdata(dev);
  908. dev_dbg(dev, "OTG PM suspend\n");
  909. return msm_otg_suspend(motg);
  910. }
  911. static int msm_otg_pm_resume(struct device *dev)
  912. {
  913. struct msm_otg *motg = dev_get_drvdata(dev);
  914. int ret;
  915. dev_dbg(dev, "OTG PM resume\n");
  916. ret = msm_otg_resume(motg);
  917. if (ret)
  918. return ret;
  919. /*
  920. * Runtime PM Documentation recommends bringing the
  921. * device to full powered state upon resume.
  922. */
  923. pm_runtime_disable(dev);
  924. pm_runtime_set_active(dev);
  925. pm_runtime_enable(dev);
  926. return 0;
  927. }
  928. #else
  929. #define msm_otg_pm_suspend NULL
  930. #define msm_otg_pm_resume NULL
  931. #endif
  932. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  933. .runtime_suspend = msm_otg_runtime_suspend,
  934. .runtime_resume = msm_otg_runtime_resume,
  935. .runtime_idle = msm_otg_runtime_idle,
  936. .suspend = msm_otg_pm_suspend,
  937. .resume = msm_otg_pm_resume,
  938. };
  939. static struct platform_driver msm_otg_driver = {
  940. .remove = __devexit_p(msm_otg_remove),
  941. .driver = {
  942. .name = DRIVER_NAME,
  943. .owner = THIS_MODULE,
  944. .pm = &msm_otg_dev_pm_ops,
  945. },
  946. };
  947. static int __init msm_otg_init(void)
  948. {
  949. return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
  950. }
  951. static void __exit msm_otg_exit(void)
  952. {
  953. platform_driver_unregister(&msm_otg_driver);
  954. }
  955. module_init(msm_otg_init);
  956. module_exit(msm_otg_exit);
  957. MODULE_LICENSE("GPL v2");
  958. MODULE_DESCRIPTION("MSM USB transceiver driver");