ux500.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "musb_core.h"
  29. struct ux500_glue {
  30. struct device *dev;
  31. struct platform_device *musb;
  32. struct clk *clk;
  33. };
  34. #define glue_to_musb(g) platform_get_drvdata(g->musb)
  35. static int ux500_musb_init(struct musb *musb)
  36. {
  37. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  38. if (IS_ERR_OR_NULL(musb->xceiv)) {
  39. pr_err("HS USB OTG: no transceiver configured\n");
  40. return -ENODEV;
  41. }
  42. return 0;
  43. }
  44. static int ux500_musb_exit(struct musb *musb)
  45. {
  46. usb_put_phy(musb->xceiv);
  47. return 0;
  48. }
  49. static const struct musb_platform_ops ux500_ops = {
  50. .init = ux500_musb_init,
  51. .exit = ux500_musb_exit,
  52. };
  53. static int __devinit ux500_probe(struct platform_device *pdev)
  54. {
  55. struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
  56. struct platform_device *musb;
  57. struct ux500_glue *glue;
  58. struct clk *clk;
  59. int ret = -ENOMEM;
  60. glue = kzalloc(sizeof(*glue), GFP_KERNEL);
  61. if (!glue) {
  62. dev_err(&pdev->dev, "failed to allocate glue context\n");
  63. goto err0;
  64. }
  65. /* get the musb id */
  66. musbid = musb_get_id(&pdev->dev, GFP_KERNEL);
  67. if (musbid < 0) {
  68. dev_err(&pdev->dev, "failed to allocate musb id\n");
  69. ret = -ENOMEM;
  70. goto err1;
  71. }
  72. musb = platform_device_alloc("musb-hdrc", musbid);
  73. if (!musb) {
  74. dev_err(&pdev->dev, "failed to allocate musb device\n");
  75. goto err2;
  76. }
  77. clk = clk_get(&pdev->dev, "usb");
  78. if (IS_ERR(clk)) {
  79. dev_err(&pdev->dev, "failed to get clock\n");
  80. ret = PTR_ERR(clk);
  81. goto err3;
  82. }
  83. ret = clk_enable(clk);
  84. if (ret) {
  85. dev_err(&pdev->dev, "failed to enable clock\n");
  86. goto err4;
  87. }
  88. musb->id = musbid;
  89. musb->dev.parent = &pdev->dev;
  90. musb->dev.dma_mask = pdev->dev.dma_mask;
  91. musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
  92. glue->dev = &pdev->dev;
  93. glue->musb = musb;
  94. glue->clk = clk;
  95. pdata->platform_ops = &ux500_ops;
  96. platform_set_drvdata(pdev, glue);
  97. ret = platform_device_add_resources(musb, pdev->resource,
  98. pdev->num_resources);
  99. if (ret) {
  100. dev_err(&pdev->dev, "failed to add resources\n");
  101. goto err5;
  102. }
  103. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  104. if (ret) {
  105. dev_err(&pdev->dev, "failed to add platform_data\n");
  106. goto err5;
  107. }
  108. ret = platform_device_add(musb);
  109. if (ret) {
  110. dev_err(&pdev->dev, "failed to register musb device\n");
  111. goto err5;
  112. }
  113. return 0;
  114. err5:
  115. clk_disable(clk);
  116. err4:
  117. clk_put(clk);
  118. err3:
  119. platform_device_put(musb);
  120. err2:
  121. musb_put_id(&pdev->dev, musbid);
  122. err1:
  123. kfree(glue);
  124. err0:
  125. return ret;
  126. }
  127. static int __devexit ux500_remove(struct platform_device *pdev)
  128. {
  129. struct ux500_glue *glue = platform_get_drvdata(pdev);
  130. musb_put_id(&pdev->dev, glue->musb->id);
  131. platform_device_del(glue->musb);
  132. platform_device_put(glue->musb);
  133. clk_disable(glue->clk);
  134. clk_put(glue->clk);
  135. kfree(glue);
  136. return 0;
  137. }
  138. #ifdef CONFIG_PM
  139. static int ux500_suspend(struct device *dev)
  140. {
  141. struct ux500_glue *glue = dev_get_drvdata(dev);
  142. struct musb *musb = glue_to_musb(glue);
  143. usb_phy_set_suspend(musb->xceiv, 1);
  144. clk_disable(glue->clk);
  145. return 0;
  146. }
  147. static int ux500_resume(struct device *dev)
  148. {
  149. struct ux500_glue *glue = dev_get_drvdata(dev);
  150. struct musb *musb = glue_to_musb(glue);
  151. int ret;
  152. ret = clk_enable(glue->clk);
  153. if (ret) {
  154. dev_err(dev, "failed to enable clock\n");
  155. return ret;
  156. }
  157. usb_phy_set_suspend(musb->xceiv, 0);
  158. return 0;
  159. }
  160. static const struct dev_pm_ops ux500_pm_ops = {
  161. .suspend = ux500_suspend,
  162. .resume = ux500_resume,
  163. };
  164. #define DEV_PM_OPS (&ux500_pm_ops)
  165. #else
  166. #define DEV_PM_OPS NULL
  167. #endif
  168. static struct platform_driver ux500_driver = {
  169. .probe = ux500_probe,
  170. .remove = __devexit_p(ux500_remove),
  171. .driver = {
  172. .name = "musb-ux500",
  173. .pm = DEV_PM_OPS,
  174. },
  175. };
  176. MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
  177. MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
  178. MODULE_LICENSE("GPL v2");
  179. static int __init ux500_init(void)
  180. {
  181. return platform_driver_register(&ux500_driver);
  182. }
  183. module_init(ux500_init);
  184. static void __exit ux500_exit(void)
  185. {
  186. platform_driver_unregister(&ux500_driver);
  187. }
  188. module_exit(ux500_exit);