dwc3-exynos.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. dev_err(dev, "not enough memory\n");
  94. return -ENOMEM;
  95. }
  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. ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
  102. if (ret)
  103. return ret;
  104. platform_set_drvdata(pdev, exynos);
  105. ret = dwc3_exynos_register_phys(exynos);
  106. if (ret) {
  107. dev_err(dev, "couldn't register PHYs\n");
  108. return ret;
  109. }
  110. clk = devm_clk_get(dev, "usbdrd30");
  111. if (IS_ERR(clk)) {
  112. dev_err(dev, "couldn't get clock\n");
  113. return -EINVAL;
  114. }
  115. exynos->dev = dev;
  116. exynos->clk = clk;
  117. clk_prepare_enable(exynos->clk);
  118. exynos->vdd33 = devm_regulator_get(dev, "vdd33");
  119. if (IS_ERR(exynos->vdd33)) {
  120. ret = PTR_ERR(exynos->vdd33);
  121. goto err2;
  122. }
  123. ret = regulator_enable(exynos->vdd33);
  124. if (ret) {
  125. dev_err(dev, "Failed to enable VDD33 supply\n");
  126. goto err2;
  127. }
  128. exynos->vdd10 = devm_regulator_get(dev, "vdd10");
  129. if (IS_ERR(exynos->vdd10)) {
  130. ret = PTR_ERR(exynos->vdd10);
  131. goto err3;
  132. }
  133. ret = regulator_enable(exynos->vdd10);
  134. if (ret) {
  135. dev_err(dev, "Failed to enable VDD10 supply\n");
  136. goto err3;
  137. }
  138. if (node) {
  139. ret = of_platform_populate(node, NULL, NULL, dev);
  140. if (ret) {
  141. dev_err(dev, "failed to add dwc3 core\n");
  142. goto err4;
  143. }
  144. } else {
  145. dev_err(dev, "no device node, failed to add dwc3 core\n");
  146. ret = -ENODEV;
  147. goto err4;
  148. }
  149. return 0;
  150. err4:
  151. regulator_disable(exynos->vdd10);
  152. err3:
  153. regulator_disable(exynos->vdd33);
  154. err2:
  155. clk_disable_unprepare(clk);
  156. return ret;
  157. }
  158. static int dwc3_exynos_remove(struct platform_device *pdev)
  159. {
  160. struct dwc3_exynos *exynos = platform_get_drvdata(pdev);
  161. device_for_each_child(&pdev->dev, NULL, dwc3_exynos_remove_child);
  162. platform_device_unregister(exynos->usb2_phy);
  163. platform_device_unregister(exynos->usb3_phy);
  164. clk_disable_unprepare(exynos->clk);
  165. regulator_disable(exynos->vdd33);
  166. regulator_disable(exynos->vdd10);
  167. return 0;
  168. }
  169. #ifdef CONFIG_OF
  170. static const struct of_device_id exynos_dwc3_match[] = {
  171. { .compatible = "samsung,exynos5250-dwusb3" },
  172. {},
  173. };
  174. MODULE_DEVICE_TABLE(of, exynos_dwc3_match);
  175. #endif
  176. #ifdef CONFIG_PM_SLEEP
  177. static int dwc3_exynos_suspend(struct device *dev)
  178. {
  179. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  180. clk_disable(exynos->clk);
  181. regulator_disable(exynos->vdd33);
  182. regulator_disable(exynos->vdd10);
  183. return 0;
  184. }
  185. static int dwc3_exynos_resume(struct device *dev)
  186. {
  187. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  188. int ret;
  189. ret = regulator_enable(exynos->vdd33);
  190. if (ret) {
  191. dev_err(dev, "Failed to enable VDD33 supply\n");
  192. return ret;
  193. }
  194. ret = regulator_enable(exynos->vdd10);
  195. if (ret) {
  196. dev_err(dev, "Failed to enable VDD10 supply\n");
  197. return ret;
  198. }
  199. clk_enable(exynos->clk);
  200. /* runtime set active to reflect active state. */
  201. pm_runtime_disable(dev);
  202. pm_runtime_set_active(dev);
  203. pm_runtime_enable(dev);
  204. return 0;
  205. }
  206. static const struct dev_pm_ops dwc3_exynos_dev_pm_ops = {
  207. SET_SYSTEM_SLEEP_PM_OPS(dwc3_exynos_suspend, dwc3_exynos_resume)
  208. };
  209. #define DEV_PM_OPS (&dwc3_exynos_dev_pm_ops)
  210. #else
  211. #define DEV_PM_OPS NULL
  212. #endif /* CONFIG_PM_SLEEP */
  213. static struct platform_driver dwc3_exynos_driver = {
  214. .probe = dwc3_exynos_probe,
  215. .remove = dwc3_exynos_remove,
  216. .driver = {
  217. .name = "exynos-dwc3",
  218. .of_match_table = of_match_ptr(exynos_dwc3_match),
  219. .pm = DEV_PM_OPS,
  220. },
  221. };
  222. module_platform_driver(dwc3_exynos_driver);
  223. MODULE_ALIAS("platform:exynos-dwc3");
  224. MODULE_AUTHOR("Anton Tikhomirov <av.tikhomirov@samsung.com>");
  225. MODULE_LICENSE("GPL v2");
  226. MODULE_DESCRIPTION("DesignWare USB3 EXYNOS Glue Layer");