ohci-exynos.c 6.9 KB

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