dwc3-exynos.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_generic.h>
  27. #include <linux/of.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/regulator/consumer.h>
  30. struct dwc3_exynos {
  31. struct platform_device *usb2_phy;
  32. struct platform_device *usb3_phy;
  33. struct device *dev;
  34. struct clk *clk;
  35. struct regulator *vdd33;
  36. struct regulator *vdd10;
  37. };
  38. static int dwc3_exynos_register_phys(struct dwc3_exynos *exynos)
  39. {
  40. struct usb_phy_generic_platform_data pdata;
  41. struct platform_device *pdev;
  42. int ret;
  43. memset(&pdata, 0x00, sizeof(pdata));
  44. pdev = platform_device_alloc("usb_phy_generic", PLATFORM_DEVID_AUTO);
  45. if (!pdev)
  46. return -ENOMEM;
  47. exynos->usb2_phy = pdev;
  48. pdata.type = USB_PHY_TYPE_USB2;
  49. pdata.gpio_reset = -1;
  50. ret = platform_device_add_data(exynos->usb2_phy, &pdata, sizeof(pdata));
  51. if (ret)
  52. goto err1;
  53. pdev = platform_device_alloc("usb_phy_generic", PLATFORM_DEVID_AUTO);
  54. if (!pdev) {
  55. ret = -ENOMEM;
  56. goto err1;
  57. }
  58. exynos->usb3_phy = pdev;
  59. pdata.type = USB_PHY_TYPE_USB3;
  60. ret = platform_device_add_data(exynos->usb3_phy, &pdata, sizeof(pdata));
  61. if (ret)
  62. goto err2;
  63. ret = platform_device_add(exynos->usb2_phy);
  64. if (ret)
  65. goto err2;
  66. ret = platform_device_add(exynos->usb3_phy);
  67. if (ret)
  68. goto err3;
  69. return 0;
  70. err3:
  71. platform_device_del(exynos->usb2_phy);
  72. err2:
  73. platform_device_put(exynos->usb3_phy);
  74. err1:
  75. platform_device_put(exynos->usb2_phy);
  76. return ret;
  77. }
  78. static int dwc3_exynos_remove_child(struct device *dev, void *unused)
  79. {
  80. struct platform_device *pdev = to_platform_device(dev);
  81. platform_device_unregister(pdev);
  82. return 0;
  83. }
  84. static int dwc3_exynos_probe(struct platform_device *pdev)
  85. {
  86. struct dwc3_exynos *exynos;
  87. struct clk *clk;
  88. struct device *dev = &pdev->dev;
  89. struct device_node *node = dev->of_node;
  90. int ret;
  91. exynos = devm_kzalloc(dev, sizeof(*exynos), GFP_KERNEL);
  92. if (!exynos)
  93. return -ENOMEM;
  94. /*
  95. * Right now device-tree probed devices don't get dma_mask set.
  96. * Since shared usb code relies on it, set it here for now.
  97. * Once we move to full device tree support this will vanish off.
  98. */
  99. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  100. if (ret)
  101. return ret;
  102. platform_set_drvdata(pdev, exynos);
  103. ret = dwc3_exynos_register_phys(exynos);
  104. if (ret) {
  105. dev_err(dev, "couldn't register PHYs\n");
  106. return ret;
  107. }
  108. clk = devm_clk_get(dev, "usbdrd30");
  109. if (IS_ERR(clk)) {
  110. dev_err(dev, "couldn't get clock\n");
  111. return -EINVAL;
  112. }
  113. exynos->dev = dev;
  114. exynos->clk = clk;
  115. clk_prepare_enable(exynos->clk);
  116. exynos->vdd33 = devm_regulator_get(dev, "vdd33");
  117. if (IS_ERR(exynos->vdd33)) {
  118. ret = PTR_ERR(exynos->vdd33);
  119. goto err2;
  120. }
  121. ret = regulator_enable(exynos->vdd33);
  122. if (ret) {
  123. dev_err(dev, "Failed to enable VDD33 supply\n");
  124. goto err2;
  125. }
  126. exynos->vdd10 = devm_regulator_get(dev, "vdd10");
  127. if (IS_ERR(exynos->vdd10)) {
  128. ret = PTR_ERR(exynos->vdd10);
  129. goto err3;
  130. }
  131. ret = regulator_enable(exynos->vdd10);
  132. if (ret) {
  133. dev_err(dev, "Failed to enable VDD10 supply\n");
  134. goto err3;
  135. }
  136. if (node) {
  137. ret = of_platform_populate(node, NULL, NULL, dev);
  138. if (ret) {
  139. dev_err(dev, "failed to add dwc3 core\n");
  140. goto err4;
  141. }
  142. } else {
  143. dev_err(dev, "no device node, failed to add dwc3 core\n");
  144. ret = -ENODEV;
  145. goto err4;
  146. }
  147. return 0;
  148. err4:
  149. regulator_disable(exynos->vdd10);
  150. err3:
  151. regulator_disable(exynos->vdd33);
  152. err2:
  153. clk_disable_unprepare(clk);
  154. return ret;
  155. }
  156. static int dwc3_exynos_remove(struct platform_device *pdev)
  157. {
  158. struct dwc3_exynos *exynos = platform_get_drvdata(pdev);
  159. device_for_each_child(&pdev->dev, NULL, dwc3_exynos_remove_child);
  160. platform_device_unregister(exynos->usb2_phy);
  161. platform_device_unregister(exynos->usb3_phy);
  162. clk_disable_unprepare(exynos->clk);
  163. regulator_disable(exynos->vdd33);
  164. regulator_disable(exynos->vdd10);
  165. return 0;
  166. }
  167. #ifdef CONFIG_OF
  168. static const struct of_device_id exynos_dwc3_match[] = {
  169. { .compatible = "samsung,exynos5250-dwusb3" },
  170. {},
  171. };
  172. MODULE_DEVICE_TABLE(of, exynos_dwc3_match);
  173. #endif
  174. #ifdef CONFIG_PM_SLEEP
  175. static int dwc3_exynos_suspend(struct device *dev)
  176. {
  177. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  178. clk_disable(exynos->clk);
  179. regulator_disable(exynos->vdd33);
  180. regulator_disable(exynos->vdd10);
  181. return 0;
  182. }
  183. static int dwc3_exynos_resume(struct device *dev)
  184. {
  185. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  186. int ret;
  187. ret = regulator_enable(exynos->vdd33);
  188. if (ret) {
  189. dev_err(dev, "Failed to enable VDD33 supply\n");
  190. return ret;
  191. }
  192. ret = regulator_enable(exynos->vdd10);
  193. if (ret) {
  194. dev_err(dev, "Failed to enable VDD10 supply\n");
  195. return ret;
  196. }
  197. clk_enable(exynos->clk);
  198. /* runtime set active to reflect active state. */
  199. pm_runtime_disable(dev);
  200. pm_runtime_set_active(dev);
  201. pm_runtime_enable(dev);
  202. return 0;
  203. }
  204. static const struct dev_pm_ops dwc3_exynos_dev_pm_ops = {
  205. SET_SYSTEM_SLEEP_PM_OPS(dwc3_exynos_suspend, dwc3_exynos_resume)
  206. };
  207. #define DEV_PM_OPS (&dwc3_exynos_dev_pm_ops)
  208. #else
  209. #define DEV_PM_OPS NULL
  210. #endif /* CONFIG_PM_SLEEP */
  211. static struct platform_driver dwc3_exynos_driver = {
  212. .probe = dwc3_exynos_probe,
  213. .remove = dwc3_exynos_remove,
  214. .driver = {
  215. .name = "exynos-dwc3",
  216. .of_match_table = of_match_ptr(exynos_dwc3_match),
  217. .pm = DEV_PM_OPS,
  218. },
  219. };
  220. module_platform_driver(dwc3_exynos_driver);
  221. MODULE_ALIAS("platform:exynos-dwc3");
  222. MODULE_AUTHOR("Anton Tikhomirov <av.tikhomirov@samsung.com>");
  223. MODULE_LICENSE("GPL v2");
  224. MODULE_DESCRIPTION("DesignWare USB3 EXYNOS Glue Layer");