ohci-exynos.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * SAMSUNG EXYNOS USB HOST OHCI Controller
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  5. * Author: Jingoo Han <jg1.han@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/usb/phy.h>
  21. #include <linux/usb/samsung_usb_phy.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/hcd.h>
  24. #include <linux/usb/otg.h>
  25. #include "ohci.h"
  26. #define DRIVER_DESC "OHCI EXYNOS driver"
  27. static const char hcd_name[] = "ohci-exynos";
  28. static struct hc_driver __read_mostly exynos_ohci_hc_driver;
  29. #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
  30. struct exynos_ohci_hcd {
  31. struct clk *clk;
  32. struct usb_phy *phy;
  33. struct usb_otg *otg;
  34. };
  35. static void exynos_ohci_phy_enable(struct platform_device *pdev)
  36. {
  37. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  38. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  39. if (exynos_ohci->phy)
  40. usb_phy_init(exynos_ohci->phy);
  41. }
  42. static void exynos_ohci_phy_disable(struct platform_device *pdev)
  43. {
  44. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  45. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  46. if (exynos_ohci->phy)
  47. usb_phy_shutdown(exynos_ohci->phy);
  48. }
  49. static int exynos_ohci_probe(struct platform_device *pdev)
  50. {
  51. struct exynos_ohci_hcd *exynos_ohci;
  52. struct usb_hcd *hcd;
  53. struct resource *res;
  54. struct usb_phy *phy;
  55. int irq;
  56. int err;
  57. /*
  58. * Right now device-tree probed devices don't get dma_mask set.
  59. * Since shared usb code relies on it, set it here for now.
  60. * Once we move to full device tree support this will vanish off.
  61. */
  62. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  63. if (err)
  64. return err;
  65. hcd = usb_create_hcd(&exynos_ohci_hc_driver,
  66. &pdev->dev, dev_name(&pdev->dev));
  67. if (!hcd) {
  68. dev_err(&pdev->dev, "Unable to create HCD\n");
  69. return -ENOMEM;
  70. }
  71. exynos_ohci = to_exynos_ohci(hcd);
  72. if (of_device_is_compatible(pdev->dev.of_node,
  73. "samsung,exynos5440-ohci"))
  74. goto skip_phy;
  75. phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  76. if (IS_ERR(phy)) {
  77. usb_put_hcd(hcd);
  78. dev_warn(&pdev->dev, "no platform data or transceiver defined\n");
  79. return -EPROBE_DEFER;
  80. } else {
  81. exynos_ohci->phy = phy;
  82. exynos_ohci->otg = phy->otg;
  83. }
  84. skip_phy:
  85. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  86. if (IS_ERR(exynos_ohci->clk)) {
  87. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  88. err = PTR_ERR(exynos_ohci->clk);
  89. goto fail_clk;
  90. }
  91. err = clk_prepare_enable(exynos_ohci->clk);
  92. if (err)
  93. goto fail_clk;
  94. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  95. if (!res) {
  96. dev_err(&pdev->dev, "Failed to get I/O memory\n");
  97. err = -ENXIO;
  98. goto fail_io;
  99. }
  100. hcd->rsrc_start = res->start;
  101. hcd->rsrc_len = resource_size(res);
  102. hcd->regs = devm_ioremap(&pdev->dev, res->start, hcd->rsrc_len);
  103. if (!hcd->regs) {
  104. dev_err(&pdev->dev, "Failed to remap I/O memory\n");
  105. err = -ENOMEM;
  106. goto fail_io;
  107. }
  108. irq = platform_get_irq(pdev, 0);
  109. if (!irq) {
  110. dev_err(&pdev->dev, "Failed to get IRQ\n");
  111. err = -ENODEV;
  112. goto fail_io;
  113. }
  114. if (exynos_ohci->otg)
  115. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  116. platform_set_drvdata(pdev, hcd);
  117. exynos_ohci_phy_enable(pdev);
  118. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  119. if (err) {
  120. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  121. goto fail_add_hcd;
  122. }
  123. device_wakeup_enable(hcd->self.controller);
  124. return 0;
  125. fail_add_hcd:
  126. exynos_ohci_phy_disable(pdev);
  127. fail_io:
  128. clk_disable_unprepare(exynos_ohci->clk);
  129. fail_clk:
  130. usb_put_hcd(hcd);
  131. return err;
  132. }
  133. static int exynos_ohci_remove(struct platform_device *pdev)
  134. {
  135. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  136. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  137. usb_remove_hcd(hcd);
  138. if (exynos_ohci->otg)
  139. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  140. exynos_ohci_phy_disable(pdev);
  141. clk_disable_unprepare(exynos_ohci->clk);
  142. usb_put_hcd(hcd);
  143. return 0;
  144. }
  145. static void exynos_ohci_shutdown(struct platform_device *pdev)
  146. {
  147. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  148. if (hcd->driver->shutdown)
  149. hcd->driver->shutdown(hcd);
  150. }
  151. #ifdef CONFIG_PM
  152. static int exynos_ohci_suspend(struct device *dev)
  153. {
  154. struct usb_hcd *hcd = dev_get_drvdata(dev);
  155. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  156. struct ohci_hcd *ohci = hcd_to_ohci(hcd);
  157. struct platform_device *pdev = to_platform_device(dev);
  158. bool do_wakeup = device_may_wakeup(dev);
  159. unsigned long flags;
  160. int rc = ohci_suspend(hcd, do_wakeup);
  161. if (rc)
  162. return rc;
  163. spin_lock_irqsave(&ohci->lock, flags);
  164. if (exynos_ohci->otg)
  165. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  166. exynos_ohci_phy_disable(pdev);
  167. clk_disable_unprepare(exynos_ohci->clk);
  168. spin_unlock_irqrestore(&ohci->lock, flags);
  169. return 0;
  170. }
  171. static int exynos_ohci_resume(struct device *dev)
  172. {
  173. struct usb_hcd *hcd = dev_get_drvdata(dev);
  174. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  175. struct platform_device *pdev = to_platform_device(dev);
  176. clk_prepare_enable(exynos_ohci->clk);
  177. if (exynos_ohci->otg)
  178. exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
  179. exynos_ohci_phy_enable(pdev);
  180. ohci_resume(hcd, false);
  181. return 0;
  182. }
  183. #else
  184. #define exynos_ohci_suspend NULL
  185. #define exynos_ohci_resume NULL
  186. #endif
  187. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  188. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  189. };
  190. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  191. .suspend = exynos_ohci_suspend,
  192. .resume = exynos_ohci_resume,
  193. };
  194. #ifdef CONFIG_OF
  195. static const struct of_device_id exynos_ohci_match[] = {
  196. { .compatible = "samsung,exynos4210-ohci" },
  197. { .compatible = "samsung,exynos5440-ohci" },
  198. {},
  199. };
  200. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  201. #endif
  202. static struct platform_driver exynos_ohci_driver = {
  203. .probe = exynos_ohci_probe,
  204. .remove = exynos_ohci_remove,
  205. .shutdown = exynos_ohci_shutdown,
  206. .driver = {
  207. .name = "exynos-ohci",
  208. .owner = THIS_MODULE,
  209. .pm = &exynos_ohci_pm_ops,
  210. .of_match_table = of_match_ptr(exynos_ohci_match),
  211. }
  212. };
  213. static int __init ohci_exynos_init(void)
  214. {
  215. if (usb_disabled())
  216. return -ENODEV;
  217. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  218. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  219. return platform_driver_register(&exynos_ohci_driver);
  220. }
  221. module_init(ohci_exynos_init);
  222. static void __exit ohci_exynos_cleanup(void)
  223. {
  224. platform_driver_unregister(&exynos_ohci_driver);
  225. }
  226. module_exit(ohci_exynos_cleanup);
  227. MODULE_ALIAS("platform:exynos-ohci");
  228. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  229. MODULE_LICENSE("GPL v2");