dwc3-exynos.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * dwc3-exynos.c - Samsung EXYNOS DWC3 Specific Glue layer
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Author: Anton Tikhomirov <av.tikhomirov@samsung.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/platform_data/dwc3-exynos.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/clk.h>
  25. #include <linux/usb/otg.h>
  26. #include <linux/usb/usb_phy_gen_xceiv.h>
  27. #include <linux/of.h>
  28. #include <linux/of_platform.h>
  29. struct dwc3_exynos {
  30. struct platform_device *usb2_phy;
  31. struct platform_device *usb3_phy;
  32. struct device *dev;
  33. struct clk *clk;
  34. };
  35. static int dwc3_exynos_register_phys(struct dwc3_exynos *exynos)
  36. {
  37. struct usb_phy_gen_xceiv_platform_data pdata;
  38. struct platform_device *pdev;
  39. int ret;
  40. memset(&pdata, 0x00, sizeof(pdata));
  41. pdev = platform_device_alloc("usb_phy_gen_xceiv", PLATFORM_DEVID_AUTO);
  42. if (!pdev)
  43. return -ENOMEM;
  44. exynos->usb2_phy = pdev;
  45. pdata.type = USB_PHY_TYPE_USB2;
  46. ret = platform_device_add_data(exynos->usb2_phy, &pdata, sizeof(pdata));
  47. if (ret)
  48. goto err1;
  49. pdev = platform_device_alloc("usb_phy_gen_xceiv", PLATFORM_DEVID_AUTO);
  50. if (!pdev) {
  51. ret = -ENOMEM;
  52. goto err1;
  53. }
  54. exynos->usb3_phy = pdev;
  55. pdata.type = USB_PHY_TYPE_USB3;
  56. ret = platform_device_add_data(exynos->usb3_phy, &pdata, sizeof(pdata));
  57. if (ret)
  58. goto err2;
  59. ret = platform_device_add(exynos->usb2_phy);
  60. if (ret)
  61. goto err2;
  62. ret = platform_device_add(exynos->usb3_phy);
  63. if (ret)
  64. goto err3;
  65. return 0;
  66. err3:
  67. platform_device_del(exynos->usb2_phy);
  68. err2:
  69. platform_device_put(exynos->usb3_phy);
  70. err1:
  71. platform_device_put(exynos->usb2_phy);
  72. return ret;
  73. }
  74. static int dwc3_exynos_remove_child(struct device *dev, void *unused)
  75. {
  76. struct platform_device *pdev = to_platform_device(dev);
  77. platform_device_unregister(pdev);
  78. return 0;
  79. }
  80. static int dwc3_exynos_probe(struct platform_device *pdev)
  81. {
  82. struct dwc3_exynos *exynos;
  83. struct clk *clk;
  84. struct device *dev = &pdev->dev;
  85. struct device_node *node = dev->of_node;
  86. int ret = -ENOMEM;
  87. exynos = devm_kzalloc(dev, sizeof(*exynos), GFP_KERNEL);
  88. if (!exynos) {
  89. dev_err(dev, "not enough memory\n");
  90. goto err1;
  91. }
  92. /*
  93. * Right now device-tree probed devices don't get dma_mask set.
  94. * Since shared usb code relies on it, set it here for now.
  95. * Once we move to full device tree support this will vanish off.
  96. */
  97. if (!dev->dma_mask)
  98. dev->dma_mask = &dev->coherent_dma_mask;
  99. if (!dev->coherent_dma_mask)
  100. dev->coherent_dma_mask = DMA_BIT_MASK(32);
  101. platform_set_drvdata(pdev, exynos);
  102. ret = dwc3_exynos_register_phys(exynos);
  103. if (ret) {
  104. dev_err(dev, "couldn't register PHYs\n");
  105. goto err1;
  106. }
  107. clk = devm_clk_get(dev, "usbdrd30");
  108. if (IS_ERR(clk)) {
  109. dev_err(dev, "couldn't get clock\n");
  110. ret = -EINVAL;
  111. goto err1;
  112. }
  113. exynos->dev = dev;
  114. exynos->clk = clk;
  115. clk_prepare_enable(exynos->clk);
  116. if (node) {
  117. ret = of_platform_populate(node, NULL, NULL, dev);
  118. if (ret) {
  119. dev_err(dev, "failed to add dwc3 core\n");
  120. goto err2;
  121. }
  122. } else {
  123. dev_err(dev, "no device node, failed to add dwc3 core\n");
  124. ret = -ENODEV;
  125. goto err2;
  126. }
  127. return 0;
  128. err2:
  129. clk_disable_unprepare(clk);
  130. err1:
  131. return ret;
  132. }
  133. static int dwc3_exynos_remove(struct platform_device *pdev)
  134. {
  135. struct dwc3_exynos *exynos = platform_get_drvdata(pdev);
  136. device_for_each_child(&pdev->dev, NULL, dwc3_exynos_remove_child);
  137. platform_device_unregister(exynos->usb2_phy);
  138. platform_device_unregister(exynos->usb3_phy);
  139. clk_disable_unprepare(exynos->clk);
  140. return 0;
  141. }
  142. #ifdef CONFIG_OF
  143. static const struct of_device_id exynos_dwc3_match[] = {
  144. { .compatible = "samsung,exynos5250-dwusb3" },
  145. {},
  146. };
  147. MODULE_DEVICE_TABLE(of, exynos_dwc3_match);
  148. #endif
  149. #ifdef CONFIG_PM_SLEEP
  150. static int dwc3_exynos_suspend(struct device *dev)
  151. {
  152. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  153. clk_disable(exynos->clk);
  154. return 0;
  155. }
  156. static int dwc3_exynos_resume(struct device *dev)
  157. {
  158. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  159. clk_enable(exynos->clk);
  160. /* runtime set active to reflect active state. */
  161. pm_runtime_disable(dev);
  162. pm_runtime_set_active(dev);
  163. pm_runtime_enable(dev);
  164. return 0;
  165. }
  166. static const struct dev_pm_ops dwc3_exynos_dev_pm_ops = {
  167. SET_SYSTEM_SLEEP_PM_OPS(dwc3_exynos_suspend, dwc3_exynos_resume)
  168. };
  169. #define DEV_PM_OPS (&dwc3_exynos_dev_pm_ops)
  170. #else
  171. #define DEV_PM_OPS NULL
  172. #endif /* CONFIG_PM_SLEEP */
  173. static struct platform_driver dwc3_exynos_driver = {
  174. .probe = dwc3_exynos_probe,
  175. .remove = dwc3_exynos_remove,
  176. .driver = {
  177. .name = "exynos-dwc3",
  178. .of_match_table = of_match_ptr(exynos_dwc3_match),
  179. .pm = DEV_PM_OPS,
  180. },
  181. };
  182. module_platform_driver(dwc3_exynos_driver);
  183. MODULE_ALIAS("platform:exynos-dwc3");
  184. MODULE_AUTHOR("Anton Tikhomirov <av.tikhomirov@samsung.com>");
  185. MODULE_LICENSE("GPL v2");
  186. MODULE_DESCRIPTION("DesignWare USB3 EXYNOS Glue Layer");