ohci-exynos.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
  112. if (err)
  113. goto fail_clk;
  114. exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
  115. if (IS_ERR(exynos_ohci->clk)) {
  116. dev_err(&pdev->dev, "Failed to get usbhost clock\n");
  117. err = PTR_ERR(exynos_ohci->clk);
  118. goto fail_clk;
  119. }
  120. err = clk_prepare_enable(exynos_ohci->clk);
  121. if (err)
  122. goto fail_clk;
  123. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  124. hcd->regs = devm_ioremap_resource(&pdev->dev, res);
  125. if (IS_ERR(hcd->regs)) {
  126. err = PTR_ERR(hcd->regs);
  127. goto fail_io;
  128. }
  129. hcd->rsrc_start = res->start;
  130. hcd->rsrc_len = resource_size(res);
  131. irq = platform_get_irq(pdev, 0);
  132. if (irq < 0) {
  133. err = irq;
  134. goto fail_io;
  135. }
  136. platform_set_drvdata(pdev, hcd);
  137. err = exynos_ohci_phy_enable(&pdev->dev);
  138. if (err) {
  139. dev_err(&pdev->dev, "Failed to enable USB phy\n");
  140. goto fail_io;
  141. }
  142. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  143. if (err) {
  144. dev_err(&pdev->dev, "Failed to add USB HCD\n");
  145. goto fail_add_hcd;
  146. }
  147. device_wakeup_enable(hcd->self.controller);
  148. return 0;
  149. fail_add_hcd:
  150. exynos_ohci_phy_disable(&pdev->dev);
  151. fail_io:
  152. clk_disable_unprepare(exynos_ohci->clk);
  153. fail_clk:
  154. usb_put_hcd(hcd);
  155. return err;
  156. }
  157. static int exynos_ohci_remove(struct platform_device *pdev)
  158. {
  159. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  160. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  161. usb_remove_hcd(hcd);
  162. exynos_ohci_phy_disable(&pdev->dev);
  163. clk_disable_unprepare(exynos_ohci->clk);
  164. usb_put_hcd(hcd);
  165. return 0;
  166. }
  167. static void exynos_ohci_shutdown(struct platform_device *pdev)
  168. {
  169. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  170. if (hcd->driver->shutdown)
  171. hcd->driver->shutdown(hcd);
  172. }
  173. #ifdef CONFIG_PM
  174. static int exynos_ohci_suspend(struct device *dev)
  175. {
  176. struct usb_hcd *hcd = dev_get_drvdata(dev);
  177. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  178. bool do_wakeup = device_may_wakeup(dev);
  179. int rc = ohci_suspend(hcd, do_wakeup);
  180. if (rc)
  181. return rc;
  182. exynos_ohci_phy_disable(dev);
  183. clk_disable_unprepare(exynos_ohci->clk);
  184. return 0;
  185. }
  186. static int exynos_ohci_resume(struct device *dev)
  187. {
  188. struct usb_hcd *hcd = dev_get_drvdata(dev);
  189. struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  190. int ret;
  191. clk_prepare_enable(exynos_ohci->clk);
  192. ret = exynos_ohci_phy_enable(dev);
  193. if (ret) {
  194. dev_err(dev, "Failed to enable USB phy\n");
  195. clk_disable_unprepare(exynos_ohci->clk);
  196. return ret;
  197. }
  198. ohci_resume(hcd, false);
  199. return 0;
  200. }
  201. #else
  202. #define exynos_ohci_suspend NULL
  203. #define exynos_ohci_resume NULL
  204. #endif
  205. static const struct ohci_driver_overrides exynos_overrides __initconst = {
  206. .extra_priv_size = sizeof(struct exynos_ohci_hcd),
  207. };
  208. static const struct dev_pm_ops exynos_ohci_pm_ops = {
  209. .suspend = exynos_ohci_suspend,
  210. .resume = exynos_ohci_resume,
  211. };
  212. #ifdef CONFIG_OF
  213. static const struct of_device_id exynos_ohci_match[] = {
  214. { .compatible = "samsung,exynos4210-ohci" },
  215. {},
  216. };
  217. MODULE_DEVICE_TABLE(of, exynos_ohci_match);
  218. #endif
  219. static struct platform_driver exynos_ohci_driver = {
  220. .probe = exynos_ohci_probe,
  221. .remove = exynos_ohci_remove,
  222. .shutdown = exynos_ohci_shutdown,
  223. .driver = {
  224. .name = "exynos-ohci",
  225. .pm = &exynos_ohci_pm_ops,
  226. .of_match_table = of_match_ptr(exynos_ohci_match),
  227. }
  228. };
  229. static int __init ohci_exynos_init(void)
  230. {
  231. if (usb_disabled())
  232. return -ENODEV;
  233. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  234. ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
  235. return platform_driver_register(&exynos_ohci_driver);
  236. }
  237. module_init(ohci_exynos_init);
  238. static void __exit ohci_exynos_cleanup(void)
  239. {
  240. platform_driver_unregister(&exynos_ohci_driver);
  241. }
  242. module_exit(ohci_exynos_cleanup);
  243. MODULE_ALIAS("platform:exynos-ohci");
  244. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  245. MODULE_LICENSE("GPL v2");