ohci-exynos.c 7.2 KB

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