ux500.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2010 ST-Ericsson AB
  3. * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  4. *
  5. * Based on omap2430.c
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/init.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <linux/io.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/usb/musb-ux500.h>
  29. #include "musb_core.h"
  30. struct ux500_glue {
  31. struct device *dev;
  32. struct platform_device *musb;
  33. struct clk *clk;
  34. };
  35. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  36. static void ux500_musb_set_vbus(struct musb *musb, int is_on)
  37. {
  38. u8 devctl;
  39. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  40. /* HDRC controls CPEN, but beware current surges during device
  41. * connect. They can trigger transient overcurrent conditions
  42. * that must be ignored.
  43. */
  44. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  45. if (is_on) {
  46. if (musb->xceiv->state == OTG_STATE_A_IDLE) {
  47. /* start the session */
  48. devctl |= MUSB_DEVCTL_SESSION;
  49. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  50. /*
  51. * Wait for the musb to set as A device to enable the
  52. * VBUS
  53. */
  54. while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
  55. if (time_after(jiffies, timeout)) {
  56. dev_err(musb->controller,
  57. "configured as A device timeout");
  58. break;
  59. }
  60. }
  61. } else {
  62. musb->is_active = 1;
  63. musb->xceiv->otg->default_a = 1;
  64. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  65. devctl |= MUSB_DEVCTL_SESSION;
  66. MUSB_HST_MODE(musb);
  67. }
  68. } else {
  69. musb->is_active = 0;
  70. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
  71. * right to B_IDLE...
  72. */
  73. musb->xceiv->otg->default_a = 0;
  74. devctl &= ~MUSB_DEVCTL_SESSION;
  75. MUSB_DEV_MODE(musb);
  76. }
  77. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  78. /*
  79. * Devctl values will be updated after vbus goes below
  80. * session_valid. The time taken depends on the capacitance
  81. * on VBUS line. The max discharge time can be upto 1 sec
  82. * as per the spec. Typically on our platform, it is 200ms
  83. */
  84. if (!is_on)
  85. mdelay(200);
  86. dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
  87. usb_otg_state_string(musb->xceiv->state),
  88. musb_readb(musb->mregs, MUSB_DEVCTL));
  89. }
  90. static int musb_otg_notifications(struct notifier_block *nb,
  91. unsigned long event, void *unused)
  92. {
  93. struct musb *musb = container_of(nb, struct musb, nb);
  94. dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
  95. event, usb_otg_state_string(musb->xceiv->state));
  96. switch (event) {
  97. case UX500_MUSB_ID:
  98. dev_dbg(musb->controller, "ID GND\n");
  99. ux500_musb_set_vbus(musb, 1);
  100. break;
  101. case UX500_MUSB_VBUS:
  102. dev_dbg(musb->controller, "VBUS Connect\n");
  103. break;
  104. case UX500_MUSB_NONE:
  105. dev_dbg(musb->controller, "VBUS Disconnect\n");
  106. if (is_host_active(musb))
  107. ux500_musb_set_vbus(musb, 0);
  108. else
  109. musb->xceiv->state = OTG_STATE_B_IDLE;
  110. break;
  111. default:
  112. dev_dbg(musb->controller, "ID float\n");
  113. return NOTIFY_DONE;
  114. }
  115. return NOTIFY_OK;
  116. }
  117. static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
  118. {
  119. unsigned long flags;
  120. irqreturn_t retval = IRQ_NONE;
  121. struct musb *musb = __hci;
  122. spin_lock_irqsave(&musb->lock, flags);
  123. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  124. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  125. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  126. if (musb->int_usb || musb->int_tx || musb->int_rx)
  127. retval = musb_interrupt(musb);
  128. spin_unlock_irqrestore(&musb->lock, flags);
  129. return retval;
  130. }
  131. static int ux500_musb_init(struct musb *musb)
  132. {
  133. int status;
  134. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  135. if (IS_ERR_OR_NULL(musb->xceiv)) {
  136. pr_err("HS USB OTG: no transceiver configured\n");
  137. return -EPROBE_DEFER;
  138. }
  139. musb->nb.notifier_call = musb_otg_notifications;
  140. status = usb_register_notifier(musb->xceiv, &musb->nb);
  141. if (status < 0) {
  142. dev_dbg(musb->controller, "notification register failed\n");
  143. return status;
  144. }
  145. musb->isr = ux500_musb_interrupt;
  146. return 0;
  147. }
  148. static int ux500_musb_exit(struct musb *musb)
  149. {
  150. usb_unregister_notifier(musb->xceiv, &musb->nb);
  151. usb_put_phy(musb->xceiv);
  152. return 0;
  153. }
  154. static const struct musb_platform_ops ux500_ops = {
  155. .init = ux500_musb_init,
  156. .exit = ux500_musb_exit,
  157. .set_vbus = ux500_musb_set_vbus,
  158. };
  159. static int ux500_probe(struct platform_device *pdev)
  160. {
  161. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  162. struct platform_device *musb;
  163. struct ux500_glue *glue;
  164. struct clk *clk;
  165. int ret = -ENOMEM;
  166. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  167. if (!glue) {
  168. dev_err(&pdev->dev, "failed to allocate glue context\n");
  169. goto err0;
  170. }
  171. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  172. if (!musb) {
  173. dev_err(&pdev->dev, "failed to allocate musb device\n");
  174. goto err1;
  175. }
  176. clk = clk_get(&pdev->dev, "usb");
  177. if (IS_ERR(clk)) {
  178. dev_err(&pdev->dev, "failed to get clock\n");
  179. ret = PTR_ERR(clk);
  180. goto err3;
  181. }
  182. ret = clk_prepare_enable(clk);
  183. if (ret) {
  184. dev_err(&pdev->dev, "failed to enable clock\n");
  185. goto err4;
  186. }
  187. musb->dev.parent = &pdev->dev;
  188. musb->dev.dma_mask = pdev->dev.dma_mask;
  189. musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
  190. glue->dev = &pdev->dev;
  191. glue->musb = musb;
  192. glue->clk = clk;
  193. pdata->platform_ops = &ux500_ops;
  194. platform_set_drvdata(pdev, glue);
  195. ret = platform_device_add_resources(musb, pdev->resource,
  196. pdev->num_resources);
  197. if (ret) {
  198. dev_err(&pdev->dev, "failed to add resources\n");
  199. goto err5;
  200. }
  201. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  202. if (ret) {
  203. dev_err(&pdev->dev, "failed to add platform_data\n");
  204. goto err5;
  205. }
  206. ret = platform_device_add(musb);
  207. if (ret) {
  208. dev_err(&pdev->dev, "failed to register musb device\n");
  209. goto err5;
  210. }
  211. return 0;
  212. err5:
  213. clk_disable_unprepare(clk);
  214. err4:
  215. clk_put(clk);
  216. err3:
  217. platform_device_put(musb);
  218. err1:
  219. kfree(glue);
  220. err0:
  221. return ret;
  222. }
  223. static int ux500_remove(struct platform_device *pdev)
  224. {
  225. struct ux500_glue *glue = platform_get_drvdata(pdev);
  226. platform_device_unregister(glue->musb);
  227. clk_disable_unprepare(glue->clk);
  228. clk_put(glue->clk);
  229. kfree(glue);
  230. return 0;
  231. }
  232. #ifdef CONFIG_PM
  233. static int ux500_suspend(struct device *dev)
  234. {
  235. struct ux500_glue *glue = dev_get_drvdata(dev);
  236. struct musb *musb = glue_to_musb(glue);
  237. usb_phy_set_suspend(musb->xceiv, 1);
  238. clk_disable_unprepare(glue->clk);
  239. return 0;
  240. }
  241. static int ux500_resume(struct device *dev)
  242. {
  243. struct ux500_glue *glue = dev_get_drvdata(dev);
  244. struct musb *musb = glue_to_musb(glue);
  245. int ret;
  246. ret = clk_prepare_enable(glue->clk);
  247. if (ret) {
  248. dev_err(dev, "failed to enable clock\n");
  249. return ret;
  250. }
  251. usb_phy_set_suspend(musb->xceiv, 0);
  252. return 0;
  253. }
  254. static const struct dev_pm_ops ux500_pm_ops = {
  255. .suspend = ux500_suspend,
  256. .resume = ux500_resume,
  257. };
  258. #define DEV_PM_OPS (&ux500_pm_ops)
  259. #else
  260. #define DEV_PM_OPS NULL
  261. #endif
  262. static struct platform_driver ux500_driver = {
  263. .probe = ux500_probe,
  264. .remove = ux500_remove,
  265. .driver = {
  266. .name = "musb-ux500",
  267. .pm = DEV_PM_OPS,
  268. },
  269. };
  270. MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
  271. MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
  272. MODULE_LICENSE("GPL v2");
  273. module_platform_driver(ux500_driver);