ohci-exynos.c 7.1 KB

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