ohci-exynos.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/phy/phy.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/hcd.h>
  23. #include "ohci.h"
  24. #define DRIVER_DESC "OHCI EXYNOS driver"
  25. static const char hcd_name[] = "ohci-exynos";
  26. static struct hc_driver __read_mostly exynos_ohci_hc_driver;
  27. #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
  28. #define PHY_NUMBER 3
  29. struct exynos_ohci_hcd {
  30. struct clk *clk;
  31. struct phy *phy[PHY_NUMBER];
  32. };
  33. static int exynos_ohci_get_phy(struct device *dev,
  34. struct exynos_ohci_hcd *exynos_ohci)
  35. {
  36. struct device_node *child;
  37. struct phy *phy;
  38. int phy_number;
  39. int ret;
  40. /* Get PHYs for the controller */
  41. for_each_available_child_of_node(dev->of_node, child) {
  42. ret = of_property_read_u32(child, "reg", &phy_number);
  43. if (ret) {
  44. dev_err(dev, "Failed to parse device tree\n");
  45. of_node_put(child);
  46. return ret;
  47. }
  48. if (phy_number >= PHY_NUMBER) {
  49. dev_err(dev, "Invalid number of PHYs\n");
  50. of_node_put(child);
  51. return -EINVAL;
  52. }
  53. phy = devm_of_phy_get(dev, child, NULL);
  54. exynos_ohci->phy[phy_number] = phy;
  55. if (IS_ERR(phy)) {
  56. ret = PTR_ERR(phy);
  57. if (ret == -EPROBE_DEFER) {
  58. return ret;
  59. } else if (ret != -ENOSYS && ret != -ENODEV) {
  60. dev_err(dev,
  61. "Error retrieving usb2 phy: %d\n", ret);
  62. return ret;
  63. }
  64. }
  65. }
  66. return 0;
  67. }
  68. static int exynos_ohci_phy_enable(struct device *dev)
  69. {
  70. struct usb_hcd *hcd = dev_get_drvdata(dev);
  71. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  72. int i;
  73. int ret = 0;
  74. for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  75. if (!IS_ERR(exynos_ohci->phy[i]))
  76. ret = phy_power_on(exynos_ohci->phy[i]);
  77. if (ret)
  78. for (i--; i >= 0; i--)
  79. if (!IS_ERR(exynos_ohci->phy[i]))
  80. phy_power_off(exynos_ohci->phy[i]);
  81. return ret;
  82. }
  83. static void exynos_ohci_phy_disable(struct device *dev)
  84. {
  85. struct usb_hcd *hcd = dev_get_drvdata(dev);
  86. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  87. int i;
  88. for (i = 0; i < PHY_NUMBER; i++)
  89. if (!IS_ERR(exynos_ohci->phy[i]))
  90. phy_power_off(exynos_ohci->phy[i]);
  91. }
  92. static int exynos_ohci_probe(struct platform_device *pdev)
  93. {
  94. struct exynos_ohci_hcd *exynos_ohci;
  95. struct usb_hcd *hcd;
  96. struct resource *res;
  97. int irq;
  98. int err;
  99. /*
  100. * Right now device-tree probed devices don't get dma_mask set.
  101. * Since shared usb code relies on it, set it here for now.
  102. * Once we move to full device tree support this will vanish off.
  103. */
  104. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  105. if (err)
  106. return err;
  107. hcd = usb_create_hcd(&exynos_ohci_hc_driver,
  108. &pdev->dev, dev_name(&pdev->dev));
  109. if (!hcd) {
  110. dev_err(&pdev->dev, "Unable to create HCD\n");
  111. return -ENOMEM;
  112. }
  113. exynos_ohci = to_exynos_ohci(hcd);
  114. if (of_device_is_compatible(pdev->dev.of_node,
  115. "samsung,exynos5440-ohci"))
  116. goto skip_phy;
  117. err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
  118. if (err)
  119. goto fail_clk;
  120. skip_phy:
  121. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  122. if (IS_ERR(exynos_ohci->clk)) {
  123. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  124. err = PTR_ERR(exynos_ohci->clk);
  125. goto fail_clk;
  126. }
  127. err = clk_prepare_enable(exynos_ohci->clk);
  128. if (err)
  129. goto fail_clk;
  130. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  131. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  132. if (IS_ERR(hcd->regs)) {
  133. err = PTR_ERR(hcd->regs);
  134. goto fail_io;
  135. }
  136. hcd->rsrc_start = res->start;
  137. hcd->rsrc_len = resource_size(res);
  138. irq = platform_get_irq(pdev, 0);
  139. if (!irq) {
  140. dev_err(&pdev->dev, "Failed to get IRQ\n");
  141. err = -ENODEV;
  142. goto fail_io;
  143. }
  144. platform_set_drvdata(pdev, hcd);
  145. err = exynos_ohci_phy_enable(&pdev->dev);
  146. if (err) {
  147. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  148. goto fail_io;
  149. }
  150. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  151. if (err) {
  152. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  153. goto fail_add_hcd;
  154. }
  155. device_wakeup_enable(hcd->self.controller);
  156. return 0;
  157. fail_add_hcd:
  158. exynos_ohci_phy_disable(&pdev->dev);
  159. fail_io:
  160. clk_disable_unprepare(exynos_ohci->clk);
  161. fail_clk:
  162. usb_put_hcd(hcd);
  163. return err;
  164. }
  165. static int exynos_ohci_remove(struct platform_device *pdev)
  166. {
  167. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  168. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  169. usb_remove_hcd(hcd);
  170. exynos_ohci_phy_disable(&pdev->dev);
  171. clk_disable_unprepare(exynos_ohci->clk);
  172. usb_put_hcd(hcd);
  173. return 0;
  174. }
  175. static void exynos_ohci_shutdown(struct platform_device *pdev)
  176. {
  177. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  178. if (hcd->driver->shutdown)
  179. hcd->driver->shutdown(hcd);
  180. }
  181. #ifdef CONFIG_PM
  182. static int exynos_ohci_suspend(struct device *dev)
  183. {
  184. struct usb_hcd *hcd = dev_get_drvdata(dev);
  185. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  186. bool do_wakeup = device_may_wakeup(dev);
  187. int rc = ohci_suspend(hcd, do_wakeup);
  188. if (rc)
  189. return rc;
  190. exynos_ohci_phy_disable(dev);
  191. clk_disable_unprepare(exynos_ohci->clk);
  192. return 0;
  193. }
  194. static int exynos_ohci_resume(struct device *dev)
  195. {
  196. struct usb_hcd *hcd = dev_get_drvdata(dev);
  197. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  198. int ret;
  199. clk_prepare_enable(exynos_ohci->clk);
  200. ret = exynos_ohci_phy_enable(dev);
  201. if (ret) {
  202. dev_err(dev, "Failed to enable USB phy\n");
  203. clk_disable_unprepare(exynos_ohci->clk);
  204. return ret;
  205. }
  206. ohci_resume(hcd, false);
  207. return 0;
  208. }
  209. #else
  210. #define exynos_ohci_suspend NULL
  211. #define exynos_ohci_resume NULL
  212. #endif
  213. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  214. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  215. };
  216. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  217. .suspend = exynos_ohci_suspend,
  218. .resume = exynos_ohci_resume,
  219. };
  220. #ifdef CONFIG_OF
  221. static const struct of_device_id exynos_ohci_match[] = {
  222. { .compatible = "samsung,exynos4210-ohci" },
  223. { .compatible = "samsung,exynos5440-ohci" },
  224. {},
  225. };
  226. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  227. #endif
  228. static struct platform_driver exynos_ohci_driver = {
  229. .probe = exynos_ohci_probe,
  230. .remove = exynos_ohci_remove,
  231. .shutdown = exynos_ohci_shutdown,
  232. .driver = {
  233. .name = "exynos-ohci",
  234. .pm = &exynos_ohci_pm_ops,
  235. .of_match_table = of_match_ptr(exynos_ohci_match),
  236. }
  237. };
  238. static int __init ohci_exynos_init(void)
  239. {
  240. if (usb_disabled())
  241. return -ENODEV;
  242. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  243. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  244. return platform_driver_register(&exynos_ohci_driver);
  245. }
  246. module_init(ohci_exynos_init);
  247. static void __exit ohci_exynos_cleanup(void)
  248. {
  249. platform_driver_unregister(&exynos_ohci_driver);
  250. }
  251. module_exit(ohci_exynos_cleanup);
  252. MODULE_ALIAS("platform:exynos-ohci");
  253. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  254. MODULE_LICENSE("GPL v2");