msm_otg.c 26 KB

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