ci13xxx_msm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /* Copyright (c) 2010-2014, The Linux Foundation. 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. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/pm_runtime.h>
  10. #include <linux/usb/msm_hsusb_hw.h>
  11. #include <linux/usb/ulpi.h>
  12. #include <linux/gpio.h>
  13. #include "ci13xxx_udc.c"
  14. #define MSM_USB_BASE (udc->regs)
  15. #define CI13XXX_MSM_MAX_LOG2_ITC 7
  16. struct ci13xxx_udc_context {
  17. int irq;
  18. void __iomem *regs;
  19. int wake_gpio;
  20. int wake_irq;
  21. bool wake_irq_state;
  22. };
  23. static struct ci13xxx_udc_context _udc_ctxt;
  24. static irqreturn_t msm_udc_irq(int irq, void *data)
  25. {
  26. return udc_irq();
  27. }
  28. static void ci13xxx_msm_suspend(void)
  29. {
  30. struct device *dev = _udc->gadget.dev.parent;
  31. dev_dbg(dev, "ci13xxx_msm_suspend\n");
  32. if (_udc_ctxt.wake_irq && !_udc_ctxt.wake_irq_state) {
  33. enable_irq_wake(_udc_ctxt.wake_irq);
  34. enable_irq(_udc_ctxt.wake_irq);
  35. _udc_ctxt.wake_irq_state = true;
  36. }
  37. }
  38. static void ci13xxx_msm_resume(void)
  39. {
  40. struct device *dev = _udc->gadget.dev.parent;
  41. dev_dbg(dev, "ci13xxx_msm_resume\n");
  42. if (_udc_ctxt.wake_irq && _udc_ctxt.wake_irq_state) {
  43. disable_irq_wake(_udc_ctxt.wake_irq);
  44. disable_irq_nosync(_udc_ctxt.wake_irq);
  45. _udc_ctxt.wake_irq_state = false;
  46. }
  47. }
  48. static void ci13xxx_msm_disconnect(void)
  49. {
  50. struct ci13xxx *udc = _udc;
  51. struct usb_phy *phy = udc->transceiver;
  52. if (phy && (phy->flags & ENABLE_DP_MANUAL_PULLUP)) {
  53. u32 temp;
  54. usb_phy_io_write(phy,
  55. ULPI_MISC_A_VBUSVLDEXT |
  56. ULPI_MISC_A_VBUSVLDEXTSEL,
  57. ULPI_CLR(ULPI_MISC_A));
  58. /* Notify LINK of VBUS LOW */
  59. temp = readl_relaxed(USB_USBCMD);
  60. temp &= ~USBCMD_SESS_VLD_CTRL;
  61. writel_relaxed(temp, USB_USBCMD);
  62. /*
  63. * Add memory barrier as it is must to complete
  64. * above USB PHY and Link register writes before
  65. * moving ahead with USB peripheral mode enumeration,
  66. * otherwise USB peripheral mode may not work.
  67. */
  68. mb();
  69. }
  70. }
  71. /* Link power management will reduce power consumption by
  72. * short time HW suspend/resume.
  73. */
  74. static void ci13xxx_msm_set_l1(struct ci13xxx *udc)
  75. {
  76. int temp;
  77. struct device *dev = udc->gadget.dev.parent;
  78. dev_dbg(dev, "Enable link power management\n");
  79. /* Enable remote wakeup and L1 for IN EPs */
  80. writel_relaxed(0xffff0000, USB_L1_EP_CTRL);
  81. temp = readl_relaxed(USB_L1_CONFIG);
  82. temp |= L1_CONFIG_LPM_EN | L1_CONFIG_REMOTE_WAKEUP |
  83. L1_CONFIG_GATE_SYS_CLK | L1_CONFIG_PHY_LPM |
  84. L1_CONFIG_PLL;
  85. writel_relaxed(temp, USB_L1_CONFIG);
  86. }
  87. static void ci13xxx_msm_connect(void)
  88. {
  89. struct ci13xxx *udc = _udc;
  90. struct usb_phy *phy = udc->transceiver;
  91. if (phy && (phy->flags & ENABLE_DP_MANUAL_PULLUP)) {
  92. int temp;
  93. usb_phy_io_write(phy,
  94. ULPI_MISC_A_VBUSVLDEXT |
  95. ULPI_MISC_A_VBUSVLDEXTSEL,
  96. ULPI_SET(ULPI_MISC_A));
  97. temp = readl_relaxed(USB_GENCONFIG2);
  98. temp |= GENCFG2_SESS_VLD_CTRL_EN;
  99. writel_relaxed(temp, USB_GENCONFIG2);
  100. temp = readl_relaxed(USB_USBCMD);
  101. temp |= USBCMD_SESS_VLD_CTRL;
  102. writel_relaxed(temp, USB_USBCMD);
  103. /*
  104. * Add memory barrier as it is must to complete
  105. * above USB PHY and Link register writes before
  106. * moving ahead with USB peripheral mode enumeration,
  107. * otherwise USB peripheral mode may not work.
  108. */
  109. mb();
  110. }
  111. }
  112. static void ci13xxx_msm_reset(void)
  113. {
  114. struct ci13xxx *udc = _udc;
  115. struct usb_phy *phy = udc->transceiver;
  116. struct device *dev = udc->gadget.dev.parent;
  117. writel_relaxed(0, USB_AHBBURST);
  118. writel_relaxed(0x08, USB_AHBMODE);
  119. if (udc->gadget.l1_supported)
  120. ci13xxx_msm_set_l1(udc);
  121. if (phy && (phy->flags & ENABLE_SECONDARY_PHY)) {
  122. int temp;
  123. dev_dbg(dev, "using secondary hsphy\n");
  124. temp = readl_relaxed(USB_PHY_CTRL2);
  125. temp |= (1<<16);
  126. writel_relaxed(temp, USB_PHY_CTRL2);
  127. /*
  128. * Add memory barrier to make sure above LINK writes are
  129. * complete before moving ahead with USB peripheral mode
  130. * enumeration.
  131. */
  132. mb();
  133. }
  134. }
  135. static void ci13xxx_msm_notify_event(struct ci13xxx *udc, unsigned event)
  136. {
  137. struct device *dev = udc->gadget.dev.parent;
  138. switch (event) {
  139. case CI13XXX_CONTROLLER_RESET_EVENT:
  140. dev_info(dev, "CI13XXX_CONTROLLER_RESET_EVENT received\n");
  141. ci13xxx_msm_reset();
  142. break;
  143. case CI13XXX_CONTROLLER_DISCONNECT_EVENT:
  144. dev_info(dev, "CI13XXX_CONTROLLER_DISCONNECT_EVENT received\n");
  145. ci13xxx_msm_disconnect();
  146. ci13xxx_msm_resume();
  147. break;
  148. case CI13XXX_CONTROLLER_CONNECT_EVENT:
  149. dev_info(dev, "CI13XXX_CONTROLLER_CONNECT_EVENT received\n");
  150. ci13xxx_msm_connect();
  151. break;
  152. case CI13XXX_CONTROLLER_SUSPEND_EVENT:
  153. dev_info(dev, "CI13XXX_CONTROLLER_SUSPEND_EVENT received\n");
  154. ci13xxx_msm_suspend();
  155. break;
  156. case CI13XXX_CONTROLLER_RESUME_EVENT:
  157. dev_info(dev, "CI13XXX_CONTROLLER_RESUME_EVENT received\n");
  158. ci13xxx_msm_resume();
  159. break;
  160. default:
  161. dev_dbg(dev, "unknown ci13xxx_udc event\n");
  162. break;
  163. }
  164. }
  165. static irqreturn_t ci13xxx_msm_resume_irq(int irq, void *data)
  166. {
  167. struct ci13xxx *udc = _udc;
  168. if (udc->transceiver && udc->vbus_active && udc->suspended)
  169. usb_phy_set_suspend(udc->transceiver, 0);
  170. else if (!udc->suspended)
  171. ci13xxx_msm_resume();
  172. return IRQ_HANDLED;
  173. }
  174. static struct ci13xxx_udc_driver ci13xxx_msm_udc_driver = {
  175. .name = "ci13xxx_msm",
  176. .flags = CI13XXX_REGS_SHARED |
  177. CI13XXX_REQUIRE_TRANSCEIVER |
  178. CI13XXX_PULLUP_ON_VBUS |
  179. CI13XXX_ZERO_ITC |
  180. CI13XXX_DISABLE_STREAMING |
  181. CI13XXX_IS_OTG,
  182. .nz_itc = 0,
  183. .notify_event = ci13xxx_msm_notify_event,
  184. };
  185. static int ci13xxx_msm_install_wake_gpio(struct platform_device *pdev,
  186. struct resource *res)
  187. {
  188. int wake_irq;
  189. int ret;
  190. dev_dbg(&pdev->dev, "ci13xxx_msm_install_wake_gpio\n");
  191. _udc_ctxt.wake_gpio = res->start;
  192. gpio_request(_udc_ctxt.wake_gpio, "USB_RESUME");
  193. gpio_direction_input(_udc_ctxt.wake_gpio);
  194. wake_irq = gpio_to_irq(_udc_ctxt.wake_gpio);
  195. if (wake_irq < 0) {
  196. dev_err(&pdev->dev, "could not register USB_RESUME GPIO.\n");
  197. return -ENXIO;
  198. }
  199. dev_dbg(&pdev->dev, "_udc_ctxt.gpio_irq = %d and irq = %d\n",
  200. _udc_ctxt.wake_gpio, wake_irq);
  201. ret = request_irq(wake_irq, ci13xxx_msm_resume_irq,
  202. IRQF_TRIGGER_RISING | IRQF_ONESHOT, "usb resume", NULL);
  203. if (ret < 0) {
  204. dev_err(&pdev->dev, "could not register USB_RESUME IRQ.\n");
  205. goto gpio_free;
  206. }
  207. disable_irq(wake_irq);
  208. _udc_ctxt.wake_irq = wake_irq;
  209. return 0;
  210. gpio_free:
  211. gpio_free(_udc_ctxt.wake_gpio);
  212. _udc_ctxt.wake_gpio = 0;
  213. return ret;
  214. }
  215. static void ci13xxx_msm_uninstall_wake_gpio(struct platform_device *pdev)
  216. {
  217. dev_dbg(&pdev->dev, "ci13xxx_msm_uninstall_wake_gpio\n");
  218. if (_udc_ctxt.wake_gpio) {
  219. gpio_free(_udc_ctxt.wake_gpio);
  220. _udc_ctxt.wake_gpio = 0;
  221. }
  222. }
  223. static int ci13xxx_msm_probe(struct platform_device *pdev)
  224. {
  225. struct resource *res;
  226. int ret;
  227. struct ci13xxx_platform_data *pdata = pdev->dev.platform_data;
  228. bool is_l1_supported = false;
  229. dev_dbg(&pdev->dev, "ci13xxx_msm_probe\n");
  230. if (pdata) {
  231. /* Acceptable values for nz_itc are: 0,1,2,4,8,16,32,64 */
  232. if (pdata->log2_itc > CI13XXX_MSM_MAX_LOG2_ITC ||
  233. pdata->log2_itc <= 0)
  234. ci13xxx_msm_udc_driver.nz_itc = 0;
  235. else
  236. ci13xxx_msm_udc_driver.nz_itc =
  237. 1 << (pdata->log2_itc-1);
  238. is_l1_supported = pdata->l1_supported;
  239. /* Set ahb2ahb bypass flag if it is requested. */
  240. if (pdata->enable_ahb2ahb_bypass)
  241. ci13xxx_msm_udc_driver.flags |=
  242. CI13XXX_ENABLE_AHB2AHB_BYPASS;
  243. }
  244. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  245. if (!res) {
  246. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  247. return -ENXIO;
  248. }
  249. _udc_ctxt.regs = ioremap(res->start, resource_size(res));
  250. if (!_udc_ctxt.regs) {
  251. dev_err(&pdev->dev, "ioremap failed\n");
  252. return -ENOMEM;
  253. }
  254. ret = udc_probe(&ci13xxx_msm_udc_driver, &pdev->dev, _udc_ctxt.regs);
  255. if (ret < 0) {
  256. dev_err(&pdev->dev, "udc_probe failed\n");
  257. goto iounmap;
  258. }
  259. _udc->gadget.l1_supported = is_l1_supported;
  260. _udc_ctxt.irq = platform_get_irq(pdev, 0);
  261. if (_udc_ctxt.irq < 0) {
  262. dev_err(&pdev->dev, "IRQ not found\n");
  263. ret = -ENXIO;
  264. goto udc_remove;
  265. }
  266. res = platform_get_resource_byname(pdev, IORESOURCE_IO, "USB_RESUME");
  267. if (res) {
  268. ret = ci13xxx_msm_install_wake_gpio(pdev, res);
  269. if (ret < 0) {
  270. dev_err(&pdev->dev, "gpio irq install failed\n");
  271. goto udc_remove;
  272. }
  273. }
  274. ret = request_irq(_udc_ctxt.irq, msm_udc_irq, IRQF_SHARED, pdev->name,
  275. pdev);
  276. if (ret < 0) {
  277. dev_err(&pdev->dev, "request_irq failed\n");
  278. goto gpio_uninstall;
  279. }
  280. pm_runtime_no_callbacks(&pdev->dev);
  281. pm_runtime_enable(&pdev->dev);
  282. return 0;
  283. gpio_uninstall:
  284. ci13xxx_msm_uninstall_wake_gpio(pdev);
  285. udc_remove:
  286. udc_remove();
  287. iounmap:
  288. iounmap(_udc_ctxt.regs);
  289. return ret;
  290. }
  291. int ci13xxx_msm_remove(struct platform_device *pdev)
  292. {
  293. pm_runtime_disable(&pdev->dev);
  294. free_irq(_udc_ctxt.irq, pdev);
  295. ci13xxx_msm_uninstall_wake_gpio(pdev);
  296. udc_remove();
  297. iounmap(_udc_ctxt.regs);
  298. return 0;
  299. }
  300. void ci13xxx_msm_shutdown(struct platform_device *pdev)
  301. {
  302. ci13xxx_pullup(&_udc->gadget, 0);
  303. }
  304. void msm_hw_bam_disable(bool bam_disable)
  305. {
  306. u32 val;
  307. struct ci13xxx *udc = _udc;
  308. if (bam_disable)
  309. val = readl_relaxed(USB_GENCONFIG) | GENCONFIG_BAM_DISABLE;
  310. else
  311. val = readl_relaxed(USB_GENCONFIG) & ~GENCONFIG_BAM_DISABLE;
  312. writel_relaxed(val, USB_GENCONFIG);
  313. }
  314. static struct platform_driver ci13xxx_msm_driver = {
  315. .probe = ci13xxx_msm_probe,
  316. .driver = {
  317. .name = "msm_hsusb",
  318. },
  319. .remove = ci13xxx_msm_remove,
  320. .shutdown = ci13xxx_msm_shutdown,
  321. };
  322. MODULE_ALIAS("platform:msm_hsusb");
  323. static int __init ci13xxx_msm_init(void)
  324. {
  325. return platform_driver_register(&ci13xxx_msm_driver);
  326. }
  327. module_init(ci13xxx_msm_init);
  328. static void __exit ci13xxx_msm_exit(void)
  329. {
  330. platform_driver_unregister(&ci13xxx_msm_driver);
  331. }
  332. module_exit(ci13xxx_msm_exit);
  333. MODULE_LICENSE("GPL v2");